Home » Microsoft Operations Manager 2005 » Restart a service if a process is missing

Restart a service if a process is missing

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


1 Comment

Comments are closed.