' Parameter 1: ServiceDisplayName (e.g. "Print Spooler") ' Parameter 2: HealthyStatus "Running", "Stopped" ' Parameter 3: [Optional] HealthyStartupType "Manual", "Auto", "Disabled" ' Unhealthy : Property[@Name='Status'] equals Error ' Healthy : Property[@Name='Status'] equals OK ' AlertText : $Data/Context/Property[@Name='AlertText']$ ' Suggested Alert Description: ' The state of the monitored service '$Target/Property[Type="MicrosoftSystemCenterNTServiceLibrary!Microsoft.SystemCenter.NTService"]/DisplayName$' on '$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/NetworkName$' is invalid: $Data/Context/Property[@Name='AlertText']$ Set oAPI = CreateObject("MOM.ScriptAPI") Set oBag = oAPI.CreatePropertyBag() Set oArgs = WScript.Arguments If oArgs.Count < 2 Then Call oAPI.LogScriptEvent("BWIN.Check.Service.vbs", 500, 0, "Script aborted. Not enough parameters provided.") WScript.Quit -1 End If ServiceDisplayName = oArgs(0) HealthyStatus = oArgs(1) Status = "OK" AlertText = "" If oArgs.Count > 2 Then ' Startuptype check HealthyStartupType = oArgs(2) ServiceStartupType = GetServiceStartupType(ServiceDisplayName) If UCase(HealthyStartupType) <> UCase(ServiceStartupType) Then Status = "Error" AlertText = "The current startup type for the service '" & ServiceDisplayName & "' is '" & ServiceStartupType & "'." & vbCrLf AlertText = AlertText & "The desired startup type is '" & HealthyStartupType & "'." & vbCrLf & vbCrLf End If End If ServiceStatus = GetServiceStatus(ServiceDisplayName) If UCase(ServiceStatus) = "NOT FOUND" Then Call oBag.AddValue("Status", "Error") Call oBag.AddValue("AlertText", "The service '" & ServiceDisplayName & "' was not found.") Call oAPI.Return(oBag) WScript.Quit - 1 ElseIf UCase(HealthyStatus) <> UCase(ServiceStatus) Then Status = "Error" AlertText = AlertText & "The current status for the service '" & ServiceDisplayName & "' is '" & ServiceStatus & "'." & vbCrLf AlertText = AlertText & "The desired status is '" & HealthyStatus & "'." & vbCrLf & vbCrLf End If Call oBag.AddValue("Status", Status) Call oBag.AddValue("AlertText", AlertText) Call oAPI.Return(oBag) Function GetServiceStatus(serviceDisplayName) ' returns "Running" if the service is started ' returns "Stopped" if the service is not started ' returns "Not Found" if the service specified is not installed GetServiceStatus = "Not Found" On Error Resume Next Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service Where DisplayName = '" & serviceDisplayName & "'") For Each objService in colRunningServices GetServiceStatus = objService.State Next On Error Goto 0 End Function Function GetServiceStartupType(serviceDisplayName) ' returns "Manual" if the service start mode is Manual ' returns "Auto" if the service start mode is Automatic ' returns "Disabled" if the service start mode is Disabled ' returns "Not Found" if the service specified is not installed GetServiceStartupType = "Not Found" On Error Resume Next Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service Where DisplayName = '" & serviceDisplayName & "'") For Each objService in colRunningServices GetServiceStartupType = objService.StartMode Next On Error Goto 0 End Function