I have to check the service in windows machines and if the service does not exists or is in stopped state then the machine should prompt a message and automatically restart after a certain period of time.
I have tried the below one but its not working.. Can someone help to get this done.
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
Best Answer
I'm not sure I understand why you want to reboot. Also, this is just batch. But:
Although, you might want to consider restarting the service instead: