Task to restart SCOM Health Service


I know many of us have always wanted a task where you were able to restart the SCOM Health service from the console.  Unfortunately this doesn't exist...until now! 🙂

I have tried in other posts to get this to work but I am sure as many of you know when you try to restart the service from a task it stops and never restarts.  This became a problem when I was trying to automate changing Management Groups between servers, etc. 

The issue is that you have to create a breakaway job from the task to allow it to one restart the service but 2. give you back data.  So in working with a customer who had to do a lot of work on his DCs and restart their services and of course many of us don't have access to those servers.  Well we do know from the from some of the built in monitors that they have recovery tasks to restart the service when they pass X threshold of Handle Count. So he actually broke out the script to get the Java script portion and I worked on getting it converted to a VBscript. 

Now you use this to do almost anything!

SCRIPT:

dim objWMIService, objProcess, strCommand
dim oShell, oShellEnv, strShell, computerName, oAPI

Set oShell = WScript.CreateObject("WScript.Shell")
Set oAPI = CreateObject("MOM.ScriptAPI")

set oShellEnv = oShell.Environment("Process")
computerName = oShellEnv("ComputerName")

strCommand = "cmd /c net stop HealthService & cmd /c net start HealthService"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Call oAPI.LogScriptEvent("SCOMTaskAgentRestart.vbs", 1001, 4, "Attempting restart of SCOM Health Service on " & computerName & " initiated by user in console")

Set objProcess = objWMIService.Get("Win32_Process")
Set objProgram = objProcess.Methods_( _
"Create").InParameters.SpawnInstance_
objProgram.CommandLine = strCommand

Set strShell = objWMIService.ExecMethod( _
"Win32_Process", "Create", objProgram)

WScript.echo "Restarted SCOM Health Service on " & computerName

 

To get this to work I created a new task and targeted the Windows Server class

and pasted the script from above with no parameters:


Now when I save and let it get pushed to all machines I can see it when I look in the windows computer view

Now to test I just select a machine and select the task

Output from the executed task. Yes, what you are assuming is correct.  It will always show that it is successful, but the difference here is that the task actually completes and doesn't just spin never knowing if it actually finished. 
If it were to fail you would receive a Heartbeat issue(just something to keep in the back of your mind) :

From the Task Status View in the console:

Now we go to the server to verify!  We are looking for a 103, 102, and 105 event at 5:36:

In later posts I will actually tie this into other tasks that I use for automation and call from Orchestrator!

Also, please feel free to leave ideas on how you think you can use this and tying it into other tasks to make the lives of us SCOM admins easier! 🙂


Disclaimer:

This example is provided “AS IS” with no warranty expressed or implied. Run at your own risk. The opinions and views expressed in this blog are those of the author and do not necessarily state or reflect those of Microsoft.

**Always test in your lab first**  Do this at your own risk!! The author will not be held responsible for any damage you incur when making these changes!

Comments (10)

  1. tvk says:

    Thanks for sharing Matty. I’m trying to implement this into a task that first removes old management Group (2007R2) and then add a 2012R2 MG, and last use this script to restart the Health service. unfortunately it does’t work with the restart of the service
    part. The change of MG work and "Restarted SCOM Health Service on " & computerName" are logged in the operations manager log as well. What kind of task are you using (agent og console task)? I edited Call oAPI.LogScriptEvent("SCOMTaskAgentRestart.vbs" so it
    matches the SCOM .vbs task script. Did try to run it with a run as account with local admin right and default action account as well. Any tips on this would be much appreciated! Thanks again.

  2. tvk says:

    Got it to work. Just some basic script errors. My bad. Thanks again for the script!

  3. Ben says:

    This is great work! It is very useful for SCOM. But can u help me with a question? I used this script as a recovery task of Monitor "health service haearbeat failure". The purpose is to restart/start the health service when a machine reported haerbeat
    failure. However, it is a little out of conotrl. When this alert got triggered, the receovery task ran as expected but it was run on the management server, not on the target server which heartbeat failed? Why this happened?

  4. Matty T says:

    Ben,
    there is already a recovery task for this associated to that monitor (Restart Health Service – towards the bottom of the recovery tasks).
    Why not use the default one instead of duplicating?

  5. anonymouscommenter says:

    A customer of mine monitors some agents that only have 1gb of storage, so disk space must be conserved

  6. Jason Aranha says:

    For Those who are interested in Writing PowerShell Script for this recovery Task Use the below parameter for the recovery Script.

    CMDDATA

    For some Reason powershell Script does`t Like Ampersand (&) in it so I have to pass value using parameter to this script.

    Then call the below command from wherever you require to reset the health service

    Start CMD $CMDDATA

    This powershell cmd will invoke an new cmd shell so the recovery task still have 20 sec to give all the output to recovery script and provide it on the SCOM Console Recovery Output,

    This is more reliable then
    Restart-Service -Name "HealthService"

    which doesn’t`t allow the recovery output & is not reliable on start the service often I have seen.

    Note : Please Test this in our environment.

  7. Jason Aranha says:

    Parameter
    Name- CMDDATA – Name
    Value -CDATA[Start cmd /C Timeout 20 & Net Stop HealthService & Net Start HealthService>-/Value
    Parameter

    For some reason the xml tag are not working so I am posting this again.

  8. Jose Fehse - MVP says:

    Thanks Matty, worked like charm. Will update my Extended SCOM agent modules soon at

    http://overcast.info/2015/11/updated-extended-agent-info-management-pack/

  9. Stephen Owen says:

    Could I use this mechanism to add a task of upgrading the SCOM Client on a remote machine? Seems like it would work!

  10. tw says:

    I created a Task using your script with the following modification to the strCommand:

    strCommand = “cmd /c net stop HealthService & cmd /c rmdir /q /s “”%ProgramFiles%\Microsoft Monitoring Agent\Agent\Health Service State”” & cmd /c net start HealthService”

    When I run the script as a Task, I can see that it actually completes what it needs to on the server I am running it on. however the Task Status in SCOM never changes from Started and it just spins. Any thoughts on why?

Skip to main content