VBS脚本来检查服务及其状态

我必须检查在Windows机器的服务,如果该服务不存在或处于停止状态,然后机器应提示一条消息,并在一段时间后自动重新启动。

我已经尝试了下面的一个,但它不工作..有人可以帮助完成这件事。

Option Explicit Const TITLE = "Service Check" Const SERVICE = "DHCP" Dim wmi Dim svcs,svc Set wmi = GetObject("winmgmts:\\.\root\cimv2") Set svcs = wmi.ExecQuery("Select * from Win32_Service where Name = 'DHCP'") If svcs.Count = 0 Then Call MsgBox(SERVICE & " service does not exist",vbCritical,Title) Call reboot(wmi) Else For Each svc In svcs If svc.State <> "Running" Then Call MsgBox(SERVICE & " service is not running",vbCritical,Title) Call reboot(wmi) End If next End If Sub reboot(ByRef wmi) Dim WSHShell Set WSHShell = WScript.CreateObject("WScript.Shell") WshShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 300" Next End Sub 

我不知道我明白你为什么要重新启动。 另外,这只是批次。 但:

 sc query DHCP | findstr /c:"RUNNING" if errorlevel 1 shutdown.exe -r -t 300 

虽然,您可能需要考虑重新启动服务:

 sc query DHCP | findstr /c:"RUNNING" if errorlevel 1 net start DHCP