If oArgs.Count <> 3 Then
g_oUtil.LogMessage _
g_oUtil.DBG_ERROR, _
"Need to provide arguments"
Quit()
End If
'==========================================================================
' Initialize the arguments in VBScript
'==========================================================================
g_oUtil.LogMessage _
g_oUtil.DBG_TRACE, _
"SourceId : " & SourceId
g_oUtil.LogMessage _
g_oUtil.DBG_TRACE, _
"ManagedEntityId : " & ManagedEntityId
g_oUtil.LogMessage _
g_oUtil.DBG_TRACE, _
"FullyQualifiedName : " & 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 <> Err.number then
g_oUtil.LogMessage _
g_oUtil.DBG_ERROR, _
"Failed with error " & Err.number & " - " & 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 <> Err.number then
g_oUtil.LogMessage _
g_oUtil.DBG_ERROR, _
"Failed with error " & Err.number & " - " & Err.Description
end if
g_oUtil.LogMessageNewLine _
g_oUtil.DBG_TRACE, _
"Finished dicovery at machine"
End Sub
'==========================================================================
' Populate Advisor Server
'==========================================================================
Public Sub Discover( _
ByRef oDiscoveryData _
)
If DoDiscovery(oDiscoveryData) >= 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']/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," _
& "authenticationLevel=pktPrivacy}!root/cimv2")
If Err.Number <> 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 <> 0 Or IsEmpty(oInstance) Then
ThrowScriptError "The class name '" & sInstance & "' returned no instances. Please check to see if this is a valid WMI class name.", Err
End If
nInstanceCount = oInstance.Count
If Err.Number <> 0 Then
ThrowScriptError "The class name '" & sInstance & "' 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 <> 0 Then
If (ErrAction And ErrAction_ThrowError) = ErrAction_ThrowError Then _
ThrowScriptErrorNoAbort "An error occurred while accessing WMI property: '" & sPropName & "'.", 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 = ""
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) & "/" & _
Mid(sValue, 7, 2) & "/" & _
Left(sValue, 4) & " " & _
Mid (sValue, 9, 2) & ":" & _
Mid(sValue, 11, 2) & ":" & _
Mid(sValue, 13, 2)
If IsDate(sTmpStrDate) Then
GetWMIProperty = CDate(sTmpStrDate)
Else
'
' Second, attempt just to convert the YYYYMMDD
'
sTmpStrDate = Mid(sValue, 5, 2) & "/" & _
Mid(sValue, 7, 2) & "/" & _
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: '" & sPropName & "'.", Err
If (ErrAction And ErrAction_Abort) = ErrAction_Abort Then _
Quit()
GetWMIProperty = ""
End If
If (ErrAction And ErrAction_Trace) = ErrAction_Trace Then _
WScript.Echo " + " & sPropName & " :: '" & GetWMIProperty & "'"
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" _
& vbCrLf &"[CustomMessage]: " & 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 >= GetCurrentLevel()) Then
if (nLevel = DBG_ERROR) Then
WScript.Echo "[Error " & CStr(Time) &"]: " & strMessage
ElseIf (nLevel = DBG_WARNING) Then
WScript.Echo "[Warning " & CStr(Time) &"]: " & strMessage
ElseIf (nLevel = DBG_TRACE) Then
WScript.Echo "[Trace " & CStr(Time) &"]: " & strMessage
End If
End If
End Sub
Public Sub LogMessageNewLine( _
ByVal nLevel, _
ByVal strMessage _
)
If (nLevel >= GetCurrentLevel()) Then
if (nLevel = DBG_ERROR) Then
WScript.Echo vbCrLf & "[Error " & CStr(Time) &"]: " & strMessage
ElseIf (nLevel = DBG_WARNING) Then
WScript.Echo vbCrLf & "[Warning " & CStr(Time) &"]: " & strMessage
ElseIf (nLevel = DBG_TRACE) Then
WScript.Echo vbCrLf & "[Trace " & CStr(Time) &"]: " & 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>