我必须检查 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
答案1
我不确定我是否理解你为什么要重新启动。另外,这只是批处理。但是:
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