' Parameters that should be passed to this script
' 0 MPElement ID ($MPElement$)
' 1 Target Id for ME this rule is running against ($Target/Id$)
' 2 Computer (FQDN) that the Network Adapter will be hosted on
' 3 Computer ID (Key) that the Network Adapter will be hosted on
Set oAPI = MOMCreateObject("MOM.ScriptAPI")
set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceID, ManagedEntityId)
If DoDiscovery(TargetComputer, TargetComputerID, oDiscoveryData, IsDiscoverDisabled) >= 0 Then
Call oAPI.Return(oDiscoveryData)
Else
Quit()
End If
End Sub
Function DoDiscovery(ByVal sTargetComputer, ByVal sTargetComputerID, ByVal oDisc, ByVal IsDiscoverDisabled)
Dim WMISet, WMISet2, owObj, owObj2, oInstance, sDeviceID
If StrComp(LCase(IsDiscoverDisabled),"true") = 0 Then
Set WMISet = WMIGetInstance("winmgmts:\\" + sTargetComputer + "\root\cimv2", "Win32_NetworkAdapter Where NetConnectionID <> null")
Else
Set WMISet = WMIGetInstance("winmgmts:\\" + sTargetComputer + "\root\cimv2", "Win32_NetworkAdapter Where NetConnectionID <> null And NetConnectionStatus <> 0")
End If
for each owObj in WMISet
Set oInstance = oDisc.CreateClassInstance("$MPElement[Name='Microsoft.Windows.Client.Win7.NetworkAdapter']$")
sDeviceID = GetWMIProperty(owObj, "DeviceID", wbemCimtypeUseDefault, ErrAction_Abort)
With oInstance
.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", sTargetComputerID
.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.LogicalDevice']/DeviceID$", sDeviceID
.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.LogicalDevice']/Name$", GetWMIProperty(owObj, "Name", wbemCimtypeUseDefault, ErrAction_None)
.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.LogicalDevice']/Description$", GetWMIProperty(owObj, "Description", wbemCimtypeUseDefault, ErrAction_None)
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/AdapterType$", GetWMIProperty(owObj, "AdapterType", wbemCimtypeUseDefault, ErrAction_None)
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/Index$", GetWMIProperty(owObj, "Index", wbemCimtypeUseDefault, ErrAction_None)
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/Manufacturer$", GetWMIProperty(owObj, "Manufacturer", wbemCimtypeUseDefault, ErrAction_None)
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/MACAddress$", Replace(GetWMIProperty(owObj, "MACAddress", wbemCimtypeUseDefault, ErrAction_None), ":", "-")
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/ServiceName$", GetWMIProperty(owObj, "ServiceName", wbemCimtypeUseDefault, ErrAction_None)
.AddProperty "$MPElement[Name='System!System.Entity']/DisplayName$", GetWMIProperty(owObj, "NetConnectionID", wbemCimtypeUseDefault, ErrAction_None)
End With
' Now get the network adapter settings
Set WMISet2 = WMIExecQuery("winmgmts:\\" + sTargetComputer + "\root\cimv2", "associators of {win32_NetworkAdapter='" & sDeviceID &"'} where ResultClass=win32_NetWorkAdapterConfiguration")
for each owObj2 in WMISet2
With oInstance
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/PerfmonInstance$", GetPerfmonInstance(GetWMIProperty(owObj2, "Description", wbemCimtypeUseDefault, ErrAction_None))
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/DHCPEnabled$", GetPerfmonInstance(GetWMIProperty(owObj2, "DHCPEnabled", wbemCimtypeUseDefault, ErrAction_None))
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/DHCPServer$", GetPerfmonInstance(GetWMIProperty(owObj2, "DHCPServer", wbemCimtypeUseDefault, ErrAction_None))
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/DNSDomain$", GetPerfmonInstance(GetWMIProperty(owObj2, "DNSDomain", wbemCimtypeUseDefault, ErrAction_None))
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/IPAddress$", GetPerfmonInstance(GetWMIProperty(owObj2, "IPAddress", wbemCimtypeUseDefault, ErrAction_None))
.AddProperty "$MPElement[Name='WindowsClient!Microsoft.Windows.Client.NetworkAdapter']/IPSubnet$", GetPerfmonInstance(GetWMIProperty(owObj2, "IPSubnet", wbemCimtypeUseDefault, ErrAction_None))
End With
Next
call oDisc.AddInstance(oInstance)
next
DoDiscovery = 0
End Function
Function WMIGetObject(ByVal sNamespace)
'
' WMIGetObject :: Returns the WMI object requested.
'
'
Dim oWMI
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error Goto 0
If IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & sNamespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
End If
Set WMIGetObject = oWMI
End Function
Function WMIGetInstance(ByVal sNamespace, ByVal sInstance)
'
' WMIGetInstance :: Returns WMI Instance requested.
'
'
Dim oWMI, oInstance, nInstanceCount
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error Goto 0
If IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & sNamespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
End If
On Error Resume Next
Set oInstance = oWMI.InstancesOf(sInstance)
e.Save
On Error Goto 0
If IsEmpty(oInstance) Or e.Number <> 0 Then
ThrowScriptError "The class name '" & sInstance & "' returned no instances. Please check to see if this is a valid WMI class name.", e
End If
'Determine if we queried a valid WMI class - Count will return 0 or empty
On Error Resume Next
nInstanceCount = oInstance.Count
e.Save
On Error Goto 0
If e.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.", e
End If
Set WMIGetInstance = oInstance
End Function
Function WMIExecQuery(ByVal sNamespace, ByVal sQuery)
'
' WMIExecQuery :: Executes the WMI query and returns the result set.
'
'
Dim oWMI, oQuery, nInstanceCount
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error Goto 0
If IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & sNamespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
End If
On Error Resume Next
Set oQuery = oWMI.ExecQuery(sQuery)
e.Save
On Error Goto 0
If IsEmpty(oQuery) Or e.Number <> 0 Then
ThrowScriptError "The Query '" & sQuery & "' returned an invalid result set. Please check to see if this is a valid WMI Query.", e
End If
'Determine if we queried a valid WMI class - Count will return 0 or empty
On Error Resume Next
nInstanceCount = oQuery.Count
e.Save
On Error Goto 0
If e.Number <> 0 Then
ThrowScriptError "The Query '" & sQuery & "' did not return any valid instances. Please check to see if this is a valid WMI Query.", e
End If
Set WMIExecQuery = oQuery
End Function
Function WMIGetInstanceNoAbort(ByVal sNamespace, ByVal sInstance)
'
' WMIGetInstanceNoAbort :: Returns WMI Instance requested.
'
'
Dim oWMI, oInstance, nInstanceCount
On Error Resume Next
Set oWMI = GetObject(sNamespace)
If Not IsEmpty(oWMI) Then
Set oInstance = oWMI.InstancesOf(sInstance)
If Not IsEmpty(oInstance) And Err.Number = 0 Then
'Determine if we queried a valid WMI class - Count will return 0 or empty
nInstanceCount = oInstance.Count
If Err.Number = 0 Then
Set WMIGetInstanceNoAbort = oInstance
Exit Function
End If
End If
End If
On Error Goto 0
Set WMIGetInstanceNoAbort = Nothing
End Function
Function GetWMIProperty(oWmi, sPropName, nCIMType, 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 = ""
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) & "/" & _
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
Class Error
Private m_lNumber
Private m_sSource
Private m_sDescription
Private m_sHelpContext
Private m_sHelpFile
Public Sub Save()
m_lNumber = Err.number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.helpfile
End Sub
Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub
Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub
Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property
End Class
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
On Error Resume Next
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
Function MomCreateObject(ByVal sProgramId)
Dim oError
Set oError = New Error
On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)
oError.Save
On Error Goto 0
If oError.Number <> 0 Then ThrowScriptError "Unable to create automation object '" & sProgramId & "'", oError
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 GetPerfmonInstance(ByVal sName)
sName = Replace(sName,"(","[")
sName = Replace(sName,")","]")
sName = Replace(sName,"/","_")
sName = Replace(sName,"#","_")
GetPerfmonInstance = sName
End Function