System.Mom.BackwardCompatibility.SubmitServiceStateEventsWriteAction (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.Mom.BackwardCompatibility.ResponseContext.Data

Member Modules:

ID Module Type TypeId RunAs 
Mapper ConditionDetection System.Mom.BackwardCompatibility.NoHost.StandardResponseContext.Mapper Default
ScriptWriteAction WriteAction System.Mom.BackwardCompatibility.ScriptWriteAction Default

Source Code:

<WriteActionModuleType ID="System.Mom.BackwardCompatibility.SubmitServiceStateEventsWriteAction" Accessibility="Internal">
<Configuration/>
<ModuleImplementation>
<Composite>
<MemberModules>
<ConditionDetection TypeID="System.Mom.BackwardCompatibility.NoHost.StandardResponseContext.Mapper" ID="Mapper">
<AlertGeneration>
<GenerateAlert>false</GenerateAlert>
</AlertGeneration>
<InvokerType>0</InvokerType>
<ManagementPackId/>
</ConditionDetection>
<WriteAction TypeID="System.Mom.BackwardCompatibility.ScriptWriteAction" ID="ScriptWriteAction">
<Body><Script>
'--------------------------------------------------------------------------
'
' Back Compat Service Monitor script
'
' This script collects the states of all services running in the system and
' submits Back Compat Service state events (event id 21207) in order for
' converted rules to properly handle service monitoring.
'
'---------------------------------------------------------------------------
Option Explicit

Dim aStateEnum
aStateEnum = Array( "Stopped", _
"Start Pending", _
"Stop Pending", _
"Running", _
"Continue Pending", _
"Pause Pending", _
"Paused")

Dim aStartupTypeEnum
aStartupTypeEnum = Array( "Boot", _
"System", _
"Auto", _
"Manual", _
"Disabled")


' Returns the State Enum numeric value as defined by MOM 2005
Function GetStateEnumValue(ByRef StateEnumString)
If IsEmpty(StateEnumString) Then
GetStateEnumValue = ""
Exit Function
End If

Dim CurrentIndex
For CurrentIndex = 0 to UBound(aStateEnum, 1)
If (StateEnumString = aStateEnum(CurrentIndex)) Then
GetStateEnumValue = CurrentIndex+1
Exit Function
End If
Next
GetStateEnumValue = 0
End Function

' Returns the Startup Type Enum numeric value as defined by MOM 2005
Function GetStartupTypeEnumValue(ByRef StartupTypeEnumString)
Dim CurrentIndex

For CurrentIndex = 0 to UBound(aStartupTypeEnum, 1)
If (StartupTypeEnumString = aStartupTypeEnum(CurrentIndex)) Then
GetStartupTypeEnumValue = CurrentIndex
Exit Function
End If
Next
GetStartupTypeEnumValue = -1
End Function

' Safely sets the event parameter and logs on failure
Sub SetServiceEventParameter(ByRef oEvent, ByRef ServiceName, ByRef PropertyName, ByRef PropertyValue)
On Error Resume Next

Call oEvent.SetEventParameter(PropertyValue)
If (Err.number &lt;&gt; 0) Then
ScriptContext.Echo "SetEventParameter failed to retrieve '" &amp; PropertyName &amp; "' for service '" &amp; ServiceName &amp; "!"
ScriptContext.Echo PropertyName &amp; " = '" &amp; PropertyValue &amp; "'"
End If

On Error Goto 0
End Sub

' Submits the MOM 2005 specified event for state monitoring
Sub SubmitServiceStateEvent(ByRef SecondsElapsed, ByRef DisplayName, ByRef OldState, ByRef NewState, ByRef ShortName, ByRef StartMode, ByRef Account)
Dim oStateEvent
Set oStateEvent = ScriptContext.CreateEvent()

With oStateEvent
.EventNumber = 21207
.EventSource = "Microsoft Operations Manager"
End With

Call SetServiceEventParameter(oStateEvent, DisplayName, "SecondsElapsed", SecondsElapsed)
Call SetServiceEventParameter(oStateEvent, DisplayName, "DisplayName", DisplayName)
Call SetServiceEventParameter(oStateEvent, DisplayName, "OldState", OldState)
Call SetServiceEventParameter(oStateEvent, DisplayName, "NewState", NewState)
Call SetServiceEventParameter(oStateEvent, DisplayName, "ShortName", ShortName)
Call SetServiceEventParameter(oStateEvent, DisplayName, "StartMode", StartMode)
Call SetServiceEventParameter(oStateEvent, DisplayName, "Account", Account)
Call SetServiceEventParameter(oStateEvent, DisplayName, "OldStateValue", GetStateEnumValue(OldState))
Call SetServiceEventParameter(oStateEvent, DisplayName, "NewStateValue", GetStateEnumValue(NewState))
Call SetServiceEventParameter(oStateEvent, DisplayName, "StartModeValue", GetStartupTypeEnumValue(StartMode))

' If there was no error setting event params, submit the event
If (Err.number = 0) Then
Call ScriptContext.Submit(oStateEvent)
End If
End Sub


Dim objWMIService
Dim colServices
Dim objCurrentService
Dim objScriptState
Dim objServiceSet

Set objWMIService = GetObject("winmgmts:" _
&amp; "{impersonationLevel=impersonate}!\\" &amp; ScriptContext.TargetFQDNComputer &amp; "\root\cimv2")

' Get the Service Set for storing/retrieving the service states
Set objScriptState = ScriptContext.GetScriptState()
Set objServiceSet = objScriptState.GetSet("__ServiceState")

' Get and save the last time from storage
Dim lastTime
lastTime = objServiceSet.get("lastTime")
Call objServiceSet.put("lastTime", Now)


' Enumerate the services and submit state events
Set colServices = objWMIService.ExecQuery("Select DisplayName, State, Name, StartMode, StartName FROM Win32_Service")
For Each objCurrentService in colServices

' Ignore services without a name
If ( Not IsNull(objCurrentService) And Not IsNull(objCurrentService.Name) And Not IsEmpty(objCurrentService.Name) ) Then

' Get and save the last State from storage
Dim oldState
oldState = objServiceSet.get(objCurrentService.Name)

' If there is a state change, update the state and submit the event
If oldState &lt;&gt; objCurrentService.State Then
Call objServiceSet.put(objCurrentService.Name, objCurrentService.State)

Call SubmitServiceStateEvent(DateDiff("s", lastTime, Now), _
objCurrentService.DisplayName, _
oldState, _
objCurrentService.State, _
objCurrentService.Name, _
objCurrentService.StartMode, _
objCurrentService.StartName)

End If
End If
Next

call objScriptState.SaveSet("__ServiceState", objServiceSet)
</Script></Body>
<Language>VBScript</Language>
<Name>MOM Backward Compatibility Service State Monitoring Script</Name>
<Parameters/>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="ScriptWriteAction">
<Node ID="Mapper"/>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System.Mom.BackwardCompatibility.ResponseContext.Data</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>