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

Contoso.se

Welcome to contoso.se! My name is Anders Bengtsson and this is my blog about Azure infrastructure and system management. I am a senior engineer in the FastTrack for Azure team, part of Azure Engineering, at Microsoft.  Contoso.se has two main purposes, first as a platform to share information with the community and the second as a notebook for myself.

Everything you read here is my own personal opinion and any code is provided "AS-IS" with no warranties.

Anders Bengtsson

MVP
MVP awarded 2007,2008,2009,2010

My Books
Service Manager Unleashed
Service Manager Unleashed
Orchestrator Unleashed
Orchestrator 2012 Unleashed
OMS
Inside the Microsoft Operations Management Suite

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.