Class VirtualExternalNetwork
Public NetworkId
Public NetworkName
Public SystemName
Public InternalDeviceId
Public ExternalDeviceId
End Class
' Subroutine to set the base class properties
Sub SetVirtualNetworkInstance(ByRef oInstance, ByVal computerId, ByVal netId, ByVal netName, ByVal port, ByVal systemName)
Call oInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", netName)
Call oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", computerId)
Call oInstance.AddProperty("$MPElement[Name='Microsoft.Windows.HyperV.ServerRole']/ServerId$", computerId)
Call oInstance.AddProperty("$MPElement[Name='Microsoft.Windows.HyperV.VirtualNetwork']/NetworkId$", netId)
Call oInstance.AddProperty("$MPElement[Name='Microsoft.Windows.HyperV.VirtualNetwork']/NetworkName$", netName)
Call oInstance.AddProperty("$MPElement[Name='Microsoft.Windows.HyperV.VirtualNetwork']/InternalDeviceId$", port)
Call oInstance.AddProperty("$MPElement[Name='Microsoft.Windows.HyperV.VirtualNetwork']/SystemName$", systemName)
End Sub
' Main Subroutine
Sub Main()
SetLocale("en-us")
InitializeCommon(1000)
' Check Arguments
Dim oArgs
Set oArgs = WScript.Arguments
If oArgs.Count < 3 Then
ThrowScriptError "Invalid parameters passed to " & GetScriptName & ".", Null
End If
' Create DiscoveryData object
Dim oAPI, oDiscoveryData, oInstance
Set oAPI = MOMCreateObject("MOM.ScriptAPI")
Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)
'Check for presence of Virtualization namespace in WMI
Dim oWMIService, errorNum
On Error Resume Next
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerIdentity & "\root\virtualization")
errorNum = Err.Number
On Error Goto 0
If (errorNum = 0) Then
' The connection sequentially is like: Virtual Network (switch) - Active Connection - SwitchLanEP - GEPSAPI - EthernetPort
Dim oVirtualSwitchList, oSwitch
Dim oActiveConectionList, oConnection
Dim oSwitchLanEPList, oSwitchLanEP
Dim oGEPSAPIList, oGEPSAI
' Ensuring existance of all objects, since all of the objects are important for communication to take place
Set oVirtualSwitchList = oWMIService.ExecQuery("SELECT Name, ElementName FROM MSVM_VirtualSwitch")
If IsListEmpty(oVirtualSwitchList) = False Then
Set oActiveConectionList = oWMIService.ExecQuery("SELECT Antecedent, Dependent FROM MSVM_ActiveConnection")
If IsListEmpty(oActiveConectionList) = False Then
Set oSwitchLanEPList = oWMIService.ExecQuery("SELECT Name FROM MSVM_SwitchLANEndpoint")
If IsListEmpty(oSwitchLanEPList) = False Then
Set oGEPSAPIList = oWMIService.ExecQuery("SELECT Antecedent, Dependent FROM MSVM_GlobalEthernetPortSAPImplementation")
If IsListEmpty(oGEPSAPIList) = False Then
' Extract Virtual External Network objects
Dim oExternalNetworkList(), oExternalNetwork, externalCount, i
Dim oExternalPortList, oExternalPort
externalCount = 0
Set oExternalPortList = oWMIService.ExecQuery("SELECT * FROM MSVM_ExternalEthernetPort WHERE IsBound=TRUE")
If IsListEmpty(oExternalPortList) = False Then
For Each oSwitch in oVirtualSwitchList
For Each oConnection in oActiveConectionList
' Antecedent of connection is connected to the virtual network
' Dependent of the connection is connected to external port
If InStr(oConnection.Antecedent, oSwitch.Name) > 0 Then
For Each oExternalPort in oExternalPortList
If InStr(oConnection.Dependent, oExternalPort.DeviceID) > 0 Then
' Add object to external network list
ReDim Preserve oExternalNetworkList(externalCount+1)
Set oExternalNetwork = New VirtualExternalNetwork
oExternalNetwork.NetworkId = oSwitch.Name
oExternalNetwork.NetworkName = oSwitch.ElementName
oExternalNetwork.SystemName = oExternalPort.SystemName
oExternalNetwork.InternalDeviceId = ""
oExternalNetwork.ExternalDeviceId = oExternalPort.DeviceID
set oExternalNetworkList(externalCount) = oExternalNetwork
externalCount = externalCount + 1
End If
Next
End If
Next
Next
End If
' Discover Virtual Internal Networks and add External and Internal Networks to Discovery Data
Dim oInternalPortList, oInternalPort
Set oInternalPortList = oWMIService.ExecQuery("SELECT DeviceID, SystemName FROM MSVM_InternalEthernetPort")
If IsListEmpty(oInternalPortList) = False Then
For Each oSwitch in oVirtualSwitchList
For Each oConnection in oActiveConectionList
If InStr(oConnection.Antecedent, oSwitch.Name) > 0 Then
For Each oSwitchLanEP in oSwitchLanEPList
' Check port for External Network
Dim isExternal
isExternal = False
For i = 0 to (externalCount-1)
If oExternalNetworkList(i).NetworkId = oSwitch.Name Then
isExternal = True
Exit For
End If
Next
' Antecedent of the connection is connected to virtual network
' Dependent of the connection is connected to SwitchLanEP
' Dependent of GEPSAI is connected to SwitchLanEP
' Antecedent of the GEPSAI is connected to internal port
If InStr(oConnection.Dependent, oSwitchLanEP.Name) Then
For Each oGEPSAI in oGEPSAPIList
If InStr(oGEPSAI.Dependent, oSwitchLanEP.Name) Then
For Each oInternalPort in oInternalPortList
If InStr(oGEPSAI.Antecedent, oInternalPort.DeviceID) > 0 Then
If IsExternal = True Then
' Search the Virtual External Network from the external network list and update its internal port
For i = 0 to (externalCount-1)
Set oExternalNetwork = oExternalNetworkList(i)
If oExternalNetwork.NetworkId = oSwitch.Name Then
oExternalNetwork.InternalDeviceId = oInternalPort.DeviceID
Exit For
End If
Next
Else
' Create a Virtual Internal Network discovery class instance
Set oInstance = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Windows.HyperV.VirtualInternalNetwork']$")
Call SetVirtualNetworkInstance(oInstance, ComputerIdentity, oSwitch.Name, oSwitch.ElementName, oInternalPort.DeviceID, oInternalPort.SystemName)
Call oDiscoveryData.AddInstance(oInstance)
End If
End If
Next
End If
Next
End If
Next
End If
Next
Next
End If
' Create a Virtual External Network discovery class instance
If (externalCount > 0) Then
For i = 0 to (externalCount-1)
Set oExternalNetwork = oExternalNetworkList(i)
Set oInstance = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Windows.HyperV.VirtualExternalNetwork']$")
Call SetVirtualNetworkInstance(oInstance, ComputerIdentity, oExternalNetwork.NetworkId, oExternalNetwork.NetworkName, oExternalNetwork.InternalDeviceId, oExternalNetwork.SystemName)
Call oInstance.AddProperty("$MPElement[Name='Microsoft.Windows.HyperV.VirtualExternalNetwork']/ExternalDeviceId$", oExternalNetwork.ExternalDeviceId)
Call oDiscoveryData.AddInstance(oInstance)
Next
End If
End If
End If
End If
End If
Else
Err.Clear
End If
Call oAPI.Return(oDiscoveryData)
End Sub
'Copyright (c) Microsoft Corporation. All rights reserved.
' 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>