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

in #vbs6 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

Coin Marketplace

STEEM 0.19
TRX 0.13
JST 0.029
BTC 65955.30
ETH 3276.19
USDT 1.00
SBD 2.70