VBS - check if service status is "running", if not kill it and start again

in #vbs7 years ago

strComputer = "."

Set ServiceSet = GetObject("winmgmts:").ExecQuery("select * from Win32_Service where Name='Spooler'")

for each Service in ServiceSet
RetVal = Service.StartService()
'wscript.echo RetVal
if not RetVal = 10 then 'control if service has state running
'WScript.Echo "Name: " & Service.Name & ", " & "Status: " & Service.Status & ", " & "ProcessId: " & Service.ProcessId & ", " & "ServiceState: " & Service.State
strProcessId = Service.ProcessId
Dim oShell, oExec, sLine
Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("taskkill /f /pid " & strProcessId)
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2:win32_service.name='Spooler'")
objWMIService.StartService
end if
next