' Function to get the NIC Device ID associated with the External Virtual Network
Function GetNICDeviceID(ByRef oWMIService, ByVal GUID)
GetNICDeviceID = -1
Dim nicList, nic
' Get the associated NIC
Set nicList = oWMIService.ExecQuery("SELECT DeviceID FROM Win32_NetworkAdapter WHERE GUID = '" & GUID & "'")
If IsListEmpty(nicList) = False Then
Set nic = nicList.ItemIndex(0)
' Get the NIC Device ID
If IsNull(nic.DeviceID) = False And nic.DeviceID > "" Then
GetNICDeviceID = nic.DeviceID
End If
End If
End Function
' Main Subroutine
Sub Main()
SetLocale("en-us")
InitializeCommon(1000)
' Check Arguments
Dim oArgs
Set oArgs = WScript.Arguments
If oArgs.Count < 5 Then
ThrowScriptError "Invalid parameters passed to " & GetScriptName & ".", Null
End If
' Create DiscoveryData object
Dim oAPI, oDiscoveryData, oInstance, oRelationship
Set oAPI = MOMCreateObject("MOM.ScriptAPI")
Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)
Dim oWMIService, errorNum
On Error Resume Next
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerIdentity & "\root\cimv2")
errorNum = Err.Number
On Error Goto 0
If (errorNum = 0) Then
Dim oNIC, nicId
nicId = GetNICDeviceID(oWMIService, PortId)
If (nicId >= 0) Then
' Create NIC instance
Set oNIC = oDiscoveryData.CreateClassInstance("$MPElement[Name='WSLib!Microsoft.Windows.Server.NetworkAdapter']$")
Call oNIC.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.LogicalDevice']/DeviceID$", nicId)
Call oNIC.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", ComputerIdentity)
' Library Initializer
Sub InitializeCommon(iScriptEventID)
'set the script event id
SCRIPT_EVENT_ID = iScriptEventID
End Sub
' List class
Class List
Private m_aData
Private m_iCount
Private m_iCapacityIncrement
' Creates an empty list.
Private Sub Class_Initialize()
m_aData = Array(0)
m_iCount = 0
m_iCapacityIncrement = 10
End Sub
' Destroys the list.
Private Sub Class_Terminate()
'deallocate the array
Redim m_aData(0)
End Sub
' Getter for data (no range checking).
Public Property Get Data(iIndex)
If IsObject(m_aData(iIndex)) Then
Set Data = m_aData(iIndex)
Else
Data = m_aData(iIndex)
End if
End property
' Setter for data (no range checking).
Public Property Let Data(iIndex, vValue)
If IsObject(vValue) Then
Set m_aData(iIndex) = vValue
Else
m_aData(iIndex) = vValue
End if
End property
' Gets the whole array.
Public Property Get DataArray()
DataArray = m_aData
End property
' Gets the number of items currently stored in the list.
Public Property Get Count()
Count = m_iCount
End property
' Gets the current capacity of the list. (Adding beyond capacity will cause the list to be resized/reallocated.
Public Property Get Capacity()
Capacity = UBound(m_aData)
End property
' Gets number of items by which the array size will be increased when it needs to be reallocated.
Public Property Get CapacityIncrement()
CapacityIncrement = m_iCapacityIncrement
End property
' Sets the number of items by which the array will be lengThened when it
' needs reallocating. (No effect If value is less than 1.)
Public Property Let CapacityIncrement(iValue)
If iValue > 0 Then
m_iCapacityIncrement = iValue
End if
End property
' Adds an item to the list. (List will be resized/reallocated as needed.)
public Function Add(vItem)
'do we have space?
If m_iCount >= UBound(m_aData) Then
'no
'reallocate array
redim preserve m_aData(UBound(m_aData) + CapacityIncrement)
End if
'add it
Data(m_iCount) = vItem
'increment count
m_iCount = m_iCount + 1
End Function
End Class
' Check the list for null or empty
Function IsListEmpty(ByVal oList)
IsListEmpty = (IsNull(oList) = True) OR (oList.Count < 1)
End Function
' Check object for null, empty or nothing
Function IsObjectUnallocated(ByVal obj)
IsObjectUnallocated = IsNull(obj) OR IsEmpty(obj) OR (TypeName(obj) = "Nothing")
End Function
' Get the Host Computer name
Function GetHostComputerName
Dim oNetwork
Set oNetwork = MOMCreateObject("WScript.Network")
GetHostComputerName = oNetwork.ComputerName
End Function
' Get the WMI object
Function GetWMIObject(ByVal namespace)
Dim oWMI
'get the object
On Error Resume Next
Set oWMI = GetObject(namespace)
If Err.Number <> 0 Or IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & namespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", Err
End If
On Error Goto 0
Set GetWMIObject = oWMI
End Function
' Get Registry object
Function GetRegistryObject(ByVal computerName)
Set GetRegistryObject = GetWMIObject("winmgmts:\\" & computerName & "\root\default:StdRegProv")
End Function
' Creates an event and sends it back to the mom server without stopping the script.
Function ThrowScriptErrorNoAbort(ByVal message, ByVal oErr)
' Retrieve the name of this (running) script
Dim scriptFileName
scriptFileName = GetScriptName
If Not IsNull(oErr) Then
message = message & " Cause: " & oErr.Description
end if
On Error Resume Next
Dim oAPITemp
Set oAPITemp = CreateObject("MOM.ScriptAPI")
If Err.Number = 0 Then
oAPITemp.LogScriptEvent scriptFileName, SCRIPT_EVENT_ID, SCOM_ERROR, message
End If
On Error Goto 0
End Function
' Creates an error event, sends it back to the mom server, and stops the script.
Function ThrowScriptError(Byval message, ByVal oErr)
On Error Resume Next
ThrowScriptErrorNoAbort message, oErr
WScript.Quit
End Function
' Gets the script name
Function GetScriptName
Dim oFSO
Set oFSO = MOMCreateObject("Scripting.FileSystemObject")
GetScriptName = oFSO.GetFile(WScript.ScriptFullName).Name
set oFSO = nothing
End Function
' Creates an object
Function MOMCreateObject(ByVal name)
'create the object
On Error Resume Next
Set MOMCreateObject = CreateObject(name)
If Err.Number <> 0 Then
ThrowScriptError "Unable to create COM object '" & name & "'", Null
End If
On Error Goto 0
End Function
' Log the message
Sub Log(message, severity)
Dim oTempAPI, scriptName
'get script name
scriptName = GetScriptName
'create a temp mom api object
'(avoids having to pass the object every time or using a global)
Set oTempAPI = MOMCreateObject("MOM.ScriptAPI")
'log event
On Error Resume Next
Call oTempAPI.LogScriptEvent(scriptName, SCRIPT_EVENT_ID, severity, message)
On Error Goto 0
End Sub </Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DataSource"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
</DataSourceModuleType>