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!