Server Role Discovery DataSource Module

Microsoft.Windows.HyperV.2012.R2.ServerRoleDiscoveryDataSourceModule (DataSourceModuleType)

Microsoft Windows Server running Hyper-V 2012 R2 Discovery DataSource Module

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.Discovery.Data

Member Modules:

ID Module Type TypeId RunAs 
DataSource DataSource Microsoft.Windows.TimedScript.DiscoveryProvider Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
IntervalSecondsint$Config/IntervalSeconds$Interval SecondsInterval Seconds
TimeoutSecondsint$Config/TimeoutSeconds$Timeout SecondsTimeout Seconds
SyncTimestring$Config/SyncTime$Synchronization TimeSynchronization Time

Source Code:

<DataSourceModuleType ID="Microsoft.Windows.HyperV.2012.R2.ServerRoleDiscoveryDataSourceModule" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="IntervalSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="0" name="SyncTime" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="SyncTime" Selector="$Config/SyncTime$" ParameterType="string"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DataSource" TypeID="Windows!Microsoft.Windows.TimedScript.DiscoveryProvider">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<SyncTime>$Config/SyncTime$</SyncTime>
<ScriptName>DiscoverHyperV2012R2ServerRole.vbs</ScriptName>
<Arguments>$MPElement$ $Target/Id$ $Target/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Arguments>
<ScriptBody><Script>
'Copyright (c) Microsoft Corporation. All rights reserved.

Option Explicit

' Constants
const WINDOWS_SERVER_VERSION_KEY = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
const WINDOWS_SERVER_VERSION_VALUE = "CurrentVersion"
const WINDOWS_SERVER_2012R2_Version = "6.3"

Call Main

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 &lt; 3 Then
ThrowScriptError "Invalid parameters passed to " &amp; GetScriptName &amp; ".", Null
End If

' Process Arguments:
' 0 - SourceId
' 1 - ManagedEntityId
' 2 - ComputerIdentity
Dim SourceID, ManagedEntityId, ComputerIdentity
SourceId = oArgs(0)
ManagedEntityId = oArgs(1)
ComputerIdentity = oArgs(2)

' 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}!\\" &amp; ComputerIdentity &amp; "\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.

' Constants
Const SCOM_ERROR = 1
Const SCOM_WARNING = 2
Const SCOM_INFORMATION = 4

Const HKEY_LOCAL_MACHINE = &amp;H80000002

Dim SCRIPT_EVENT_ID

' 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 &gt; 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 &gt;= 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 &lt; 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 &lt;&gt; 0 Or IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" &amp; namespace &amp; "'. 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:\\" &amp; computerName &amp; "\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 &amp; " Cause: " &amp; 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 &lt;&gt; 0 Then
ThrowScriptError "Unable to create COM object '" &amp; name &amp; "'", 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>