Windows Service Discovery

AVIcode.DotNet.SystemCenter.DotNetWinServiceApplication.WMIDiscovery.Probe (ProbeActionModuleType)

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
DataSource ProbeAction Microsoft.Windows.ScriptPropertyBagProbe Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
TimeoutSecondsint$Config/TimeoutSeconds$Timeout SecondsModule execution timeout (measured in seconds).
SafeEnablebool$Config/SafeEnable$Discovery EnabledFor safe discovery disabling. Do not use standard 'Enable' parameter to disable the rule.
DiscoveryEnablebool$Config/DiscoveryEnable$Discovery Enabled For Sub GroupFor safe discovery disabling. Do not use standard 'Enable' parameter to disable the rule.

Source Code:

<ProbeActionModuleType ID="AVIcode.DotNet.SystemCenter.DotNetWinServiceApplication.WMIDiscovery.Probe" Accessibility="Public">
<Configuration>
<xsd:element name="ServiceName" type="xsd:string"/>
<xsd:element name="TimeoutSeconds" type="xsd:integer"/>
<xsd:element name="SafeEnable" type="xsd:boolean"/>
<xsd:element name="DiscoveryEnable" type="xsd:boolean"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="TimeoutSeconds" ParameterType="int" Selector="$Config/TimeoutSeconds$"/>
<OverrideableParameter ID="SafeEnable" ParameterType="bool" Selector="$Config/SafeEnable$"/>
<OverrideableParameter ID="DiscoveryEnable" ParameterType="bool" Selector="$Config/DiscoveryEnable$"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<ProbeAction ID="DataSource" TypeID="Windows!Microsoft.Windows.ScriptPropertyBagProbe">
<ScriptName>DotNetWinServiceApplication.WMIDiscovery.PA.vbs</ScriptName>
<Arguments>"$Config/ServiceName$" "$Config/SafeEnable$" "$Config/DiscoveryEnable$"</Arguments>
<ScriptBody><Script>
On Error Resume Next

SetLocale("en-us")
HandleErrorContinue("Cannot set en-us locale")

Dim WShell : Set WShell = CreateObject("wscript.shell")
HandleErrorWithoutPropertyBag("CreateObject('wscript.shell')")

Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
HandleErrorWithoutPropertyBag("CreateObject('Scripting.FileSystemObject')")

Dim oAPI : Set oAPI = CreateObject("MOM.ScriptAPI")
HandleErrorWithoutPropertyBag("MOM.ScriptAPI")

Dim Args : Set Args = WScript.Arguments
if Args.Count &lt; 3 then
WScript.Quit
end if

Dim ServiceName : ServiceName = Args(0)
Dim safeEnable : safeEnable = Args(1)
Dim discoveryEnable : discoveryEnable = Args(2)

Dim oMOMBag : Set oMOMBag = oAPI.CreatePropertyBag()
HandleErrorWithoutPropertyBag("Cannot create PropertyBag")
If Not safeEnable or Not discoveryEnable Then
ReturnWithoutObject()
End If

Dim WMI_NAMESPACE : WMI_NAMESPACE = "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"
Dim WMI_SERVICE : WMI_SERVICE = "SELECT * FROM Win32_Service WHERE Name = '" &amp; ServiceName &amp; "'"

Dim objWMI : Set objWMI = GetObject(WMI_NAMESPACE)
HandleError("Cannot connect to WMI")

Dim serviceObj : Set serviceObj = objWMI.ExecQuery(WMI_SERVICE)
HandleError("Cannot retrieve windows service from WMI: " &amp; ServiceName)

Dim found : found = FALSE
For Each service in serviceObj
Call oMOMBag.AddValue("Name", service.Name)
Call oMOMBag.AddValue("PathName", service.PathName)
Call oMOMBag.AddValue("DisplayName", service.DisplayName)

If service.Description &lt;&gt; null and Len(Trim(service.Description)) &gt; 0 Then
Call oMOMBag.AddValue("Description", service.Description)
Else
Call oMOMBag.AddValue("Description", "Empty")
End If

Call oMOMBag.AddValue("IsEmptyInstance", "false")
found = TRUE
Next
HandleError("Cannot create property bag data")

If NOT found Then
ReturnWithoutObject()
End If

Call oAPI.Return(oMOMBag)

Function ReturnWithoutObject()
Call oMOMBag.AddValue("IsEmptyInstance", "true")
Call oAPI.Return(oMOMBag)
Wscript.Quit 0
End Function

Sub HandleError(customMessage)
If Not (Err.number = 0) Then
LogError customMessage
ReturnWithoutObject
End If
End Sub

Sub HandleErrorWithoutPropertyBag(customMessage)
If Not (Err.number = 0) Then
LogError customMessage
Wscript.Quit 0
End If
End Sub

Function HandleErrorContinue(customMessage)
HandleErrorContinue = False
If Not (Err.number = 0) Then
LogError customMessage
Err.Clear
HandleErrorContinue = True
End If
End Function

Sub LogError(customMessage)
Dim msg
If Not (Err.number = 0) Then
msg = Replace(" Error: #P1# Description: #P2# ", "#P1#", CStr(Err.number) )
msg = Replace(msg, "#P2#", Err.Description )
msg = customMessage &amp; VbCrLf &amp; msg &amp; VbCrLf
If UCase(Hex(Err.number)) = UCase("80041006") Then
msg = msg &amp; "WMI Error. Not enough memory for the operation." &amp; VbCrLf
End If
WScript.StdOut.WriteLine msg
msg = msg &amp; " [" &amp; ScriptInfo() &amp; "]"
LogEvent msg, 1
End If
End Sub

Function ScriptInfo()
Dim commandLineInfo : commandLineInfo = WScript.ScriptFullName
Dim argument
For Each argument In WScript.Arguments
commandLineInfo = commandLineInfo &amp; " """ &amp; argument &amp; """"
Next
ScriptInfo = commandLineInfo
End Function

Sub LogEvent (message, eventType)
Dim errorEventId : errorEventId = 20000
Dim oAPI0 : Set oAPI0 = CreateObject("MOM.ScriptAPI")
Call oAPI0.LogScriptEvent("AVIcode .NET Enterprise Management Pack for OpsMgr 2007", errorEventId, eventType, message)
End Sub
</Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="DataSource"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
<InputType>System!System.BaseData</InputType>
</ProbeActionModuleType>