Sub Main
' Setup initial configuration
SetLocale("en-us")
InitializeCommon(1000)
Dim errorNum
errorNum = 0
' 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 OS Version
Dim oReg, osVersion
Set oReg = GetRegistryObject(ComputerIdentity)
osVersion = ""
If IsObjectUnallocated(oReg) = False Then
On Error Resume Next
oReg.GetStringValue HKEY_LOCAL_MACHINE, WINDOWS_SERVER_VERSION_KEY, WINDOWS_SERVER_VERSION_VALUE, osVersion
On Error Goto 0
End If
' Check for Windows Server 2012R2
If (osVersion = WINDOWS_SERVER_2012R2_Version) Then
'Check for presence of Virtualization namespace in WMI
Dim oWMIService
On Error Resume Next
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerIdentity & "\root\virtualization\v2")
errorNum = Err.Number
On Error Goto 0
If (errorNum = 0) Then
' Get Server object from WMI and create discovery class instance
Dim oServer, oServerList
Set oServerList = oWMIService.ExecQuery("SELECT Name FROM MSVM_ComputerSystem WHERE Name = '" + GetHostComputerName() + "'")
If (IsListEmpty(oServerList) = False) Then
Set oServer = oServerList.ItemIndex(0)
If IsObjectUnallocated(oServer) = False Then
Set oInstance = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Windows.HyperV.2012.R2.ServerRole']$")
Call oInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", oServer.Name)
Call oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", ComputerIdentity)
Call oInstance.AddProperty("$MPElement[Name='HVLib!Microsoft.Windows.HyperV.ServerRole']/ServerId$", ComputerIdentity)
Call oInstance.AddProperty("$MPElement[Name='HVLib!Microsoft.Windows.HyperV.ServerRole']/ServerName$", oServer.Name)
Call oDiscoveryData.AddInstance(oInstance)
End If
End If
End If
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>