This is a script that will restart a service if a process is missing. I this example the DFS service will be restarted if not notepad.exe is running. Maybe not a very likely scenario :)Â
strProcess = "notepad.exe" 'Name of the process
strServiceName = "DFS" 'Name of the service
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objShell = CreateObject("Wscript.Shell")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strProcess & "'")
If colProcesses.Count < 1 Then
Const EVENT_WARNING = 2
Set objShell = CreateObject("Wscript.Shell")
objShell.LogEvent 2, strProcess & " is not running. The following process will be restarted: " & strServiceName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name = '" & strServiceName & "'")
For Each objService in colListOfServices
If objService.State = "Running" Then
objService.StopService
strNow = now
Do Until DateDiff("s", strNow, now) > 10
Loop
End IF
objService.StartService
Next
End If
[…] Server process alert? Great!! I have the script on my website http://contoso.se/blog/?p=110 , in swedish, but I think you will get which part that is the script. — Regards Anders Bengtsson […]