Restart a service

Here is a script that will monitor a service. If the service is “stopped” it will be restarted and a event will be generated in the local event viewer. You can create a rule to collect event ID 4 and/or event source WSH, then you will get information about it in Operators Console.

strServiceName = "Alerter"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name = '" & strServiceName & "'")
For Each objService in colListOfServices
If objService.State = "Stopped" Then
Set objShell = CreateObject("Wscript.Shell")
objShell.LogEvent 4, strServiceName & " has been restarted."

End If

2 thoughts on “Restart a service

Leave a Reply

Your email address will not be published. Required fields are marked *