Microsoft System Center Advisor Windows Server Discovery Script Probe

Microsoft.SystemCenter.Advisor.Windows.Server.Computer.Discovery.New.ScriptProbe (ProbeActionModuleType)

Microsoft System Center Advisor Windows Server Discovery population script

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.Discovery.Data

Member Modules:

ID Module Type TypeId RunAs 
Script ProbeAction Microsoft.Windows.ScriptDiscoveryProbe Default

Source Code:

<ProbeActionModuleType ID="Microsoft.SystemCenter.Advisor.Windows.Server.Computer.Discovery.New.ScriptProbe" Accessibility="Internal" Batching="false" PassThrough="false">
<Configuration/>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<ProbeAction ID="Script" TypeID="Windows!Microsoft.Windows.ScriptDiscoveryProbe">
<ScriptName>PopulateAdvisorWindowsServerProperties.vbs</ScriptName>
<Arguments>$MPElement$ $Target/Host/Id$ $Target/Host/Property[Type='Windows!Microsoft.Windows.Computer']/PrincipalName$</Arguments>
<ScriptBody><Script>

Option Explicit

SetLocale("en-us")

Const wbemCimtypeUseDefault = 0 'Use Default Type CIM type - Custom
Const wbemCimtypeSint16 = 2 'Signed 16-bit integer
Const wbemCimtypeSint32 = 3 'Signed 32-bit integer
Const wbemCimtypeReal32 = 4 '32-bit real number
Const wbemCimtypeReal64 = 5 '64-bit real number
Const wbemCimtypeString = 8 'String
Const wbemCimtypeBoolean = 11 'Boolean value
Const wbemCimtypeObject = 13 'CIM object
Const wbemCimtypeSint8 = 16 'Signed 8-bit integer
Const wbemCimtypeUint8 = 17 'Unsigned 8-bit integer
Const wbemCimtypeUint16 = 18 'Unsigned 16-bit integer
Const wbemCimtypeUint32 = 19 'Unsigned 32-bit integer
Const wbemCimtypeSint64 = 20 'Signed 64-bit integer
Const wbemCimtypeUint64 = 21 'Unsigned 64-bit integer
Const wbemCimtypeDatetime = 101 'Date/time value
Const wbemCimtypeReference = 102 'Reference to a CIM object
Const wbemCimtypeChar16 = 103 '16-bit character

Const ErrAction_None = 0
Const ErrAction_Trace = 1
Const ErrAction_ThrowError = 16
Const ErrAction_Abort = 32
Const ErrAction_ThrowErrorAndAbort = 48

Dim g_oUtil
Set g_oUtil = New Util

Call g_oUtil.SetDebugLevel(g_oUtil.DBG_NONE)

Dim oArgs
Set oArgs = WScript.Arguments

If oArgs.Count &lt;&gt; 3 Then
g_oUtil.LogMessage _
g_oUtil.DBG_ERROR, _
"Need to provide arguments"

Quit()
End If

'==========================================================================
' Initialize the arguments in VBScript
'==========================================================================

Dim SourceId, ManagedEntityId, FullyQualifiedName

SourceId = oArgs(0)
ManagedEntityId = oArgs(1)
FullyQualifiedName = oArgs(2)

g_oUtil.LogMessage _
g_oUtil.DBG_TRACE, _
"SourceId : " &amp; SourceId
g_oUtil.LogMessage _
g_oUtil.DBG_TRACE, _
"ManagedEntityId : " &amp; ManagedEntityId
g_oUtil.LogMessage _
g_oUtil.DBG_TRACE, _
"FullyQualifiedName : " &amp; FullyQualifiedName

'==========================================================================
' Main
'==========================================================================

Dim oAPI
Set oAPI = CreateObject("MOM.ScriptAPI")
If oAPI is Nothing Then
g_oUtil.LogMessage _
g_oUtil.DBG_ERROR, _
"Failed to get Discovery API. OpsMgr must be installed to execute this script"

if 0 &lt;&gt; Err.number then
g_oUtil.LogMessage _
g_oUtil.DBG_ERROR, _
"Failed with error " &amp; Err.number &amp; " - " &amp; Err.Description
end if

End If

Call DiscoverInstances()
Wscript.Quit(0)

'==========================================================================
' FUNCTIONS
'==========================================================================
Sub DiscoverInstances()

g_oUtil.LogMessage _
g_oUtil.DBG_TRACE, _
"Started dicovery at machine"

Dim oDiscoveryData
Set oDiscoveryData = oAPI.CreateDiscoveryData ( _
0, _
SourceId, _
ManagedEntityId _
)

If oDiscoveryData is Nothing Then
g_oUtil.LogMessage _
g_oUtil.DBG_ERROR, _
"Failed to get Discovery Data Object. OpsMgr must be installed to execute this script"

if 0 &lt;&gt; Err.number then
g_oUtil.LogMessage _
g_oUtil.DBG_ERROR, _
"Failed with error " &amp; Err.number &amp; " - " &amp; Err.Description
end if

Exit Sub
End If

Call Discover(oDiscoveryData)
Call oAPI.Return(oDiscoveryData)

g_oUtil.LogMessageNewLine _
g_oUtil.DBG_TRACE, _
"Finished dicovery at machine"

End Sub

'==========================================================================
' Populate Advisor Server
'==========================================================================
Public Sub Discover( _
ByRef oDiscoveryData _
)

If DoDiscovery(oDiscoveryData) &gt;= 0 Then
Call oAPI.Return(oDiscoveryData)
Else
Quit()
End If

End Sub

Function DoDiscovery(_
ByRef oDiscoveryData _
)

g_oUtil.LogMessage _
g_oUtil.DBG_TRACE, _
"Getting Win32_Processor instances from machine"

Dim WMISet, owObj
Set WMISet = WMIGetInstance("Win32_Processor")

Dim NumberOfCores
NumberOfCores = 0
Dim NumberOfLogicalProcessors
NumberOfLogicalProcessors = 0

for each owObj in WMISet
NumberOfCores = NumberOfCores + CLng(GetWMIProperty(owObj, "NumberOfCores", ErrAction_Abort))
NumberOfLogicalProcessors = NumberOfLogicalProcessors + CLng(GetWMIProperty(owObj, "NumberOfLogicalProcessors", ErrAction_Abort))
next

g_oUtil.LogMessage _
g_oUtil.DBG_TRACE, _
"Creating class instance"

Dim oInstance
Set oInstance = oDiscoveryData.CreateClassInstance( _
"$MPElement[Name='AdvCore!Microsoft.SystemCenter.Advisor.Windows.Server.Computer']$" _
)

g_oUtil.LogMessage _
g_oUtil.DBG_TRACE, _
"Populating class instance"

With oInstance
.AddProperty _
"$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", _
FullyQualifiedName

.AddProperty _
"$MPElement[Name='AdvCore!Microsoft.SystemCenter.Advisor.Windows.Server.Computer']/NumberOfCores$", _
NumberOfCores

.AddProperty _
"$MPElement[Name='AdvCore!Microsoft.SystemCenter.Advisor.Windows.Server.Computer']/NumberOfLogicalProcessors$", _
NumberOfLogicalProcessors
End With

Call oDiscoveryData.AddInstance(oInstance)
DoDiscovery = 0
End Function

Function Quit()
WScript.Quit(-1)
End Function

Function WMIGetInstance(ByVal sInstance)
'
' WMIGetInstance :: Returns WMI Instance requested.
'
'
Dim oWMI, oInstance, nInstanceCount

On Error Resume Next

Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate," _
&amp; "authenticationLevel=pktPrivacy}!root/cimv2")

If Err.Number &lt;&gt; 0 Or IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", Err
End If

Set oInstance = oWMI.InstancesOf(sInstance)
If Err.Number &lt;&gt; 0 Or IsEmpty(oInstance) Then
ThrowScriptError "The class name '" &amp; sInstance &amp; "' returned no instances. Please check to see if this is a valid WMI class name.", Err
End If

nInstanceCount = oInstance.Count
If Err.Number &lt;&gt; 0 Then
ThrowScriptError "The class name '" &amp; sInstance &amp; "' did not return any valid instances. Please check to see if this is a valid WMI class name.", Err
End If

Set WMIGetInstance = oInstance

End Function

Function GetWMIProperty(oWmi, sPropName, ErrAction)
Dim sValue, oWmiProp

If Not IsValidObject(oWmi) Then
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "Accessing property on invalid WMI object.", Err

If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
Quit()

GetWMIProperty = ""
Exit Function
End If

On Error Resume Next
Set oWmiProp = oWmi.Properties_.Item(sPropName)
If Err.Number &lt;&gt; 0 Then
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "An error occurred while accessing WMI property: '" &amp; sPropName &amp; "'.", Err

If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
Quit()
End If
On Error Goto 0

If IsValidObject(oWmiProp) Then
sValue = oWmiProp.Value

If IsNull(sValue) Then
'
' If value is null, return blank to avoid any issues
'
GetWMIProperty = ""

Else

Select Case (oWmiProp.CIMType)
Case wbemCimtypeString, _
wbemCimtypeSint16, _
wbemCimtypeSint32, _
wbemCimtypeReal32, _
wbemCimtypeReal64, _
wbemCimtypeSint8, _
wbemCimtypeUint8, _
wbemCimtypeUint16, _
wbemCimtypeUint32, _
wbemCimtypeSint64, _
wbemCimtypeUint64:

If Not oWmiProp.IsArray Then
GetWMIProperty = Trim(CStr(sValue))
Else
GetWMIProperty = Join(sValue, ", ")
End If

Case wbemCimtypeBoolean:

If sValue = 1 Or UCase(sValue) = "TRUE" Then
GetWMIProperty = "True"
Else
GetWMIProperty = "False"
End If

Case wbemCimtypeDatetime:

Dim sTmpStrDate

'
' First attempt to convert the whole wmi date string
'
sTmpStrDate = Mid(sValue, 5, 2) &amp; "/" &amp; _
Mid(sValue, 7, 2) &amp; "/" &amp; _
Left(sValue, 4) &amp; " " &amp; _
Mid (sValue, 9, 2) &amp; ":" &amp; _
Mid(sValue, 11, 2) &amp; ":" &amp; _
Mid(sValue, 13, 2)
If IsDate(sTmpStrDate) Then
GetWMIProperty = CDate(sTmpStrDate)
Else

'
' Second, attempt just to convert the YYYYMMDD
'
sTmpStrDate = Mid(sValue, 5, 2) &amp; "/" &amp; _
Mid(sValue, 7, 2) &amp; "/" &amp; _
Left(sValue, 4)
If IsDate(sTmpStrDate) Then
GetWMIProperty = CDate(sTmpStrDate)
Else
'
' Nothing works - return passed in string
'
GetWMIProperty = sValue
End If

End If

Case Else:
GetWMIProperty = ""
End Select
End If
Else

If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "An error occurred while accessing WMI property: '" &amp; sPropName &amp; "'.", Err

If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
Quit()

GetWMIProperty = ""

End If


If (ErrAction And ErrAction_Trace) = ErrAction_Trace Then _
WScript.Echo " + " &amp; sPropName &amp; " :: '" &amp; GetWMIProperty &amp; "'"

End Function

Function IsValidObject(ByVal oObject)
IsValidObject = False

If IsObject(oObject) Then
If Not oObject Is Nothing Then
IsValidObject = True
End If
End If
End Function

Function ThrowScriptErrorNoAbort(ByVal sMessage, ByVal oErr)
'
' ThrowScriptError :: Creates an event and sends it back to the mom server
'

Dim sErrDescription, sErrNumber
sErrDescription = oErr.Description
sErrNumber = oErr.Number

g_oUtil.LogMessage _
g_oUtil.DBG_ERROR, _
"Error raised on the machine" _
&amp; vbCrLf &amp;"[CustomMessage]: " &amp; sMessage

End Function

Function ThrowScriptError(Byval sMessage, ByVal oErr)
'
' ThrowScriptError :: Creates an event and sends it back to the mom server
'
'
On Error Resume Next
ThrowScriptErrorNoAbort sMessage, oErr
Quit()

End Function

'==========================================================================
' Class: Util
' Description: Utility methods for logging
'==========================================================================
Class Util

' Used to say to LogMessage when/how to print the message.
Public DBG_NONE
Public DBG_ERROR
Public DBG_WARNING
Public DBG_TRACE

'Internal Debug Level
Private m_nDebugLevel

'======================================================================
' Method: Class_Initialize
' Description: This is the constructor
' Parameters:
'======================================================================
Private Sub Class_Initialize()
' Initialize Debug level constants
DBG_TRACE = 1
DBG_WARNING = 2
DBG_ERROR = 3
DBG_NONE = 4

'by default only errors are logged
m_nDebugLevel = DBG_ERROR

End Sub

'======================================================================
' Method: GetCurrentLevel
' Description: retrieve current logging level.
' Parameters:
'======================================================================
Public Function GetCurrentLevel()
GetCurrentLevel = m_nDebugLevel
End Function

'======================================================================
' Method: SetDebugLevel
' Description: To change the debugging output level of information
' generated by this utility.
' Parameters:
' nLevel - DBG_NONE, DBG_TRACE, DBG_WARNING or DBG_ERROR
'======================================================================
Public Sub SetDebugLevel(ByVal nLevel)
m_nDebugLevel = nLevel
End Sub

'======================================================================
' Method: LogMessage
' Description: Log a debug message to ScriptContext
' Parameters:
' nLevel - Debug level for the message that
' we're logging.
' strMessage - The message to write to the trace.
'======================================================================
Public Sub LogMessage( _
ByVal nLevel, _
ByVal strMessage _
)
If (nLevel &gt;= GetCurrentLevel()) Then
if (nLevel = DBG_ERROR) Then
WScript.Echo "[Error " &amp; CStr(Time) &amp;"]: " &amp; strMessage
ElseIf (nLevel = DBG_WARNING) Then
WScript.Echo "[Warning " &amp; CStr(Time) &amp;"]: " &amp; strMessage
ElseIf (nLevel = DBG_TRACE) Then
WScript.Echo "[Trace " &amp; CStr(Time) &amp;"]: " &amp; strMessage
End If
End If
End Sub
Public Sub LogMessageNewLine( _
ByVal nLevel, _
ByVal strMessage _
)
If (nLevel &gt;= GetCurrentLevel()) Then
if (nLevel = DBG_ERROR) Then
WScript.Echo vbCrLf &amp; "[Error " &amp; CStr(Time) &amp;"]: " &amp; strMessage
ElseIf (nLevel = DBG_WARNING) Then
WScript.Echo vbCrLf &amp; "[Warning " &amp; CStr(Time) &amp;"]: " &amp; strMessage
ElseIf (nLevel = DBG_TRACE) Then
WScript.Echo vbCrLf &amp; "[Trace " &amp; CStr(Time) &amp;"]: " &amp; strMessage
End If
End If
End Sub

End Class ' Util
</Script></ScriptBody>
<TimeoutSeconds>300</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="Script"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
<InputType>System!System.BaseData</InputType>
</ProbeActionModuleType>