Microsoft.SystemCenter.VirtualMachineManager.2007.VMMHostG2FDiscovery (DataSourceModuleType)

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.Discovery.Data

Member Modules:

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

Overrideable Parameters:

IDParameterTypeSelector
IntervalSecondsint$Config/IntervalSeconds$

Source Code:

<DataSourceModuleType ID="Microsoft.SystemCenter.VirtualMachineManager.2007.VMMHostG2FDiscovery" Accessibility="Internal">
<Configuration>
<xsd:element name="IntervalSeconds" type="xsd:integer"/>
<xsd:element name="ComputerID" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" ParameterType="int" Selector="$Config/IntervalSeconds$"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedScript.DiscoveryProvider">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<SyncTime>00:25</SyncTime>
<ScriptName>DiscoverVMMHostG2F.vbs</ScriptName>
<Arguments>$MPElement$ $Target/Id$ $Config/ComputerID$</Arguments>
<ScriptBody><Script>
' Copyright (c) Microsoft Corporation. All rights reserved.
' VBScript source code
' DiscoverVMMHostG2F.vbs
' Arg 0 : SourceID
' Arg 1 : MP Element ID
' Arg 2 : Computer ID
Option Explicit

' Reg string const
Const StrVMMManagementGroupInstallationRegKey = "HKLM\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Server\Setup\InstallPath"
Const StrVirtualServerRegKey = "HKLM\System\CurrentControlSet\Services\Virtual Server\Start"
Const StrVMMServerInstallationRegKey = "HKLM\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Server\Setup\InstallPath"
Const StrVMMSelfServiceServerInstallationRegKey = "HKLM\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Self-Service Portal\Setup\InstallPath"
Const StrVMMSSsiteEngineMachineRegKey = "HKLM\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Self-Service Portal\Settings\VmmServerName"
Const StrVMMDatabaseServerRegKey = "HKLM\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Server\Settings\Sql\OnRemoteServer"
Const StrVMMDatabaseNameRegKey = "HKLM\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Server\Settings\Sql\DatabaseName"
Const StrVMMDatabaseInstanceRegKey = "HKLM\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Server\Settings\Sql\InstanceName"
Const StrVMMRemoteDatabaseMachineFQDNRegKey = "HKLM\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Server\Settings\Sql\MachineFQDN"
Const StrVMMDatabaseConnectionStringRegKey = "HKLM\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Server\Settings\Sql\ConnectionString"
Const StrWebsitDisplayName = "Microsoft System Center Virtual Machine Manager 2007 Self-Service Portal"
Const EnableMOMTracingRegKey = "HKLM\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Server\Setup\Enable_MOM_Tracing"

Const StrSQLServerDefaultInstance = "MSSQLSERVER"
Const StrEmpty = ""
Const StrFolderSeparator = "\"

Const StrLocationHost = "Host"
Const StrLocationLibrary = "Library"

'=============
' Method: HasRegistry
' Description: This function returns true if the registry exists; otherwise returns false
'=============
Function HasRegistry(ByVal regString)
Dim bHasRegistry
Dim oReg
Set oReg = CreateObject("WScript.Shell")

On Error Resume Next
Dim sValue
sValue = oReg.RegRead(regString)
If Err.number &lt;&gt; 0 Then
bHasRegistry = False
Else
bHasRegistry = True
End If
On Error Goto 0

HasRegistry = bHasRegistry
End Function

'=============
' Method: ReadRegistry
' Description: This function reads the registry, returns true if the registry exists; otherwise returns false
'=============
Function ReadRegistry(ByVal regString, ByRef regValue)
Dim bHasRegistry
Dim oReg
Set oReg = CreateObject("WScript.Shell")

On Error Resume Next
regValue = oReg.RegRead(regString)
If Err.number &lt;&gt; 0 Then
bHasRegistry = False
Else
bHasRegistry = True
End If
On Error Goto 0

ReadRegistry = bHasRegistry
End Function

Call Main()

'=============
' Method: Main
' Description: This sub discovers ManagedHost instance and adds its contains relationship with HostGroup;
' It also discovers VM on the ManagedHost and adds their relationship.
'=============
Sub Main()
Dim oArgs
Set oArgs = WScript.Arguments
if oArgs.Count &lt;&gt; 3 Then
WScript.Quit()
End If

Dim SourceID, ManagedEntityId, TargetComputer
SourceId = oArgs(0)
ManagedEntityId = oArgs(1)
TargetComputer = oArgs(2)

Dim oAPI, oDiscoveryData
Set oAPI = CreateObject("MOM.ScriptAPI")
set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)

If HasRegistry(EnableMOMTracingRegKey) Then
oAPI.LogScriptEvent "DiscoverVMMHostG2F.vbs runs", 1, 2, "TargetComputer " &amp; TargetComputer
End If

' Open Database connection
Dim cnADOConnection
OpenADOConnection cnADOConnection

If Not IsNull(cnADOConnection) Then
Dim sParentID
sParentID = Null
Dim oVMMManagementGroupInst
oVMMManagementGroupInst = Null
' Discover HostGroup, ManagedHost add relationship
DiscoverVMMHostGroup oDiscoveryData, cnADOConnection, oVMMManagementGroupInst, sParentID, StrEmpty, TargetComputer

cnADOConnection.Close
End If

Call oAPI.Return(oDiscoveryData)
End Sub

'=============
' Method: OpenADOConnection
' Description: This sub ADOConnect to databasse
'=============
Sub OpenADOConnection(ByRef connection)
On Error Resume Next
Dim cnctADOConnection
Set cnctADOConnection = CreateObject("ADODB.Connection")
cnctADOConnection.Provider = "sqloledb"
cnctADOConnection.ConnectionTimeout = 80

Dim connectionString
If ReadRegistry(StrVMMDatabaseConnectionStringRegKey,connectionString) Then
cnctADOConnection.Open connectionString
if Err.Number &lt;&gt; 0 then
connection = Null
Else
set connection = cnctADOConnection
end if
Else
connection = Null
End If
On Error Goto 0
End Sub

'=============
' Method: DiscoverVMMHostGroup
' Description: This sub recursively discovers HostGroup, and discovers ManagedHost and adds relationship
'=============
Sub DiscoverVMMHostGroup(ByRef oDiscoveredData, ByRef cnADOConnection, ByRef oParentHostGroupInst, ByVal sParentHostGroupId, ByVal sParentHostGroupName, ByVal hostComputer)
' Discover HostGroup
On Error Resume Next
Dim oHostGroupResults
Dim sQuery
If IsNull(sParentHostGroupId) Then 'It is the root HostGroup
sQuery = "select name, description, ID from tbl_BTBS_HostGroup where ParentID is null"
Else
sQuery = "select name, description, ID from tbl_BTBS_HostGroup where ParentID='" &amp; sParentHostGroupId &amp; "'"
End If
Set oHostGroupResults = cnADOConnection.Execute(sQuery)
If Err.Number = 0 Then
Do while Not oHostGroupResults.EOF
Dim oHostGroupInst, sHostGroupName, sHostGroupId
sHostGroupName = sParentHostGroupName &amp; StrFolderSeparator &amp; oHostGroupResults(0) 'Prefix with parent's name
sHostGroupId = oHostGroupResults(2)
set oHostGroupInst = AddHostGroupInst(oDiscoveredData, hostComputer, sHostGroupName, sHostGroupId, oHostGroupResults(1))
' call itself to find nested HostGroup
DiscoverVMMHostGroup oDiscoveredData, cnADOConnection, oHostGroupInst, oHostGroupResults(2), sHostGroupName, hostComputer
' Discover VMMManagedHost and VMMVirtualMachine under the this HostGroup
DiscoverVMMManagedHostAndVM oDiscoveredData, cnADOConnection, oHostGroupInst, sHostGroupName, sHostGroupId, hostComputer

oHostGroupResults.MoveNext
Loop
end if
On Error Goto 0
End Sub

'=============
' Method: AddHostGroupInst
' Description: This function initializes and adds one HostGroup instance and returns the referenc to this instance
'=============
Function AddHostGroupInst(ByRef oDiscoveredData, ByVal hostComputer, ByVal sHostGroupName, ByVal sHostGroupId, ByVal sHostGroupDscrp)
Dim oHostGroupInst
set oHostGroupInst = oDiscoveredData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.VirtualMachineManager.2007.HostGroup']$")
With oHostGroupInst
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.HostGroup']/ServerMachine$", hostComputer
.AddProperty "$MPElement[Name='Microsoft.SystemCenter.VirtualMachineManager.2007.HostGroup']/Name$", sHostGroupName
End With
call oDiscoveredData.AddInstance(oHostGroupInst)
set AddHostGroupInst = oHostGroupInst
End Function

'=============
' Method: DiscoverVMMManagedHostAndVM
' Description: This sub discovers ManagedHost
'=============
Sub DiscoverVMMManagedHostAndVM(ByRef oDiscoveredData, ByRef cnADOConnection, ByRef oHostGroupInst, ByVal hostGroupName, ByVal sHostGroupId, ByVal vMMComputer)
' Discover ManagedHost
On Error Resume Next
Dim oHostResults, oVMAgentResults, oVMMAgentInst, version, agentVersionState
Set oHostResults = cnADOConnection.Execute("select ComputerName, Description, HostID, TotalMemory, VirtualServerVersionState from tbl_ADHC_Host where HostGroupId='" &amp; sHostGroupId &amp; "'")
If Err.Number = 0 Then
Dim sHostComputerName, sDescription, sHostId, noOfVMsDeployed, noOfVMsRunning, totalMemory
Dim sHostIdFirstChar, sHostIdSecondChar
Do while Not oHostResults.EOF
' Add HostComputer instance
sHostComputerName = oHostResults(0)
sDescription = oHostResults(1)
sHostId = oHostResults(2)
totalMemory = oHostResults(3)
sHostIdFirstChar = Right(Left(UCase(sHostId),2), 1) ' The id is like {1de...3}
sHostIdSecondChar = Right(Left(UCase(sHostId),3), 1)
If (sHostIdFirstChar = "2") and (sHostIdSecondChar &gt; "7") Then 'classify the Hosts into 32 groups, this is the 6th group
noOfVMsDeployed = NumberOfVMsDeployed(cnADOConnection, sHostId)
noOfVMsRunning = NumberOfVMsRunning(cnADOConnection, sHostId)
version = ""
Set oVMAgentResults = cnADOConnection.Execute("select tbl_ADHC_AgentServer.AgentVersion, tbl_ADHC_AgentServer.AgentVersionState from tbl_ADHC_AgentServerRelation, tbl_ADHC_AgentServer Where tbl_ADHC_AgentServerRelation.AgentServerID = tbl_ADHC_AgentServer.AgentServerID and tbl_ADHC_AgentServerRelation.HostLibraryServerID = '" &amp; sHostId &amp; "'")
If Err.Number = 0 Then
Do while Not oVMAgentResults.EOF
version = oVMAgentResults(0)
agentVersionState = oVMAgentResults(1)
oVMAgentResults.MoveNext
Loop
End If
Dim oHostInst
set oHostInst = AddManagedHostInst(oDiscoveredData, sHostComputerName, hostGroupName, vMMComputer, version, sDescription, totalMemory, noOfVMsDeployed, noOfVMsRunning, oHostResults(4), agentVersionState, sHostId)

' Add HostGroupContainsManagedHost relationship
AddRelationShip oDiscoveredData, oHostGroupInst, oHostInst, "$MPElement[Name='Microsoft.SystemCenter.VirtualMachineManager.2007.HostGroupContainsManagedHost']$"

' Discover the Hosted VirtualMachine instance
DiscoverVMMVirtualMachine oDiscoveredData, cnADOConnection, oHostInst, sHostId, sHostComputerName, vMMComputer
End If

oHostResults.MoveNext
Loop
end if
On Error Goto 0
End Sub

'=============
' Method: NumberOfVMsDeployed
' Description: This function queries the carmine database and returns the number of VMs hosted by a specific host
'=============
Function NumberOfVMsDeployed(ByRef aDOConnection, ByVal hostId)
Dim oVMsDeployed
Set oVMsDeployed = aDOConnection.Execute("select count(*) from tbl_IL_Object where ObjectType=1 and HostID='" &amp; hostId &amp; "'")
If Not IsEmpty(oVMsDeployed) Then
Do while Not oVMsDeployed.EOF
NumberOfVMsDeployed = oVMsDeployed(0)
oVMsDeployed.MoveNext
Loop
Else
NumberOfVMsDeployed = number
End If
End Function

'=============
' Method: NumberOfVMsRunning
' Description: This function queries the carmine database and returns the number of running VMs hosted by a specific host
'=============
Function NumberOfVMsRunning(ByRef aDOConnection, ByVal hostId)
Dim oVMsRunning
Set oVMsRunning = aDOConnection.Execute("select count(*) from tbl_IL_Object where ObjectType=1 and ObjectState=0 and HostID='" &amp; hostId &amp; "'")
If Not IsEmpty(oVMsRunning) Then
Do while Not oVMsRunning.EOF
NumberOfVMsRunning = oVMsRunning(0)
oVMsRunning.MoveNext
Loop
Else
NumberOfVMsRunning = 0
End If
End Function

'=============
' Method: AddManagedHostInst
' Description: This function initializes and adds one ManagedHost instance and returns the referenc to this instance
'=============
Function AddManagedHostInst(ByRef oDiscoveredData, ByVal hostComputer, ByVal hostGroupName, ByVal vMMServer, ByVal version, ByVal description, ByVal totalRAM, ByVal noOfVMsDeployed, ByVal noOfVMsRunning, ByVal virtualServerVersionState, ByVal agentVersionState, ByVal hostId)
Dim oHostInst
set oHostInst = oDiscoveredData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.VirtualMachineManager.2007.ManagedHost']$")
With oHostInst
.AddProperty "$MPElement[Name='System!System.Entity']/DisplayName$", hostComputer
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/Guid$", hostId
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/VMMServer$", vMMServer
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/Description$", description
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/Version$", version
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/HostGroup$", hostGroupName
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/HostName$", hostComputer
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/TotalRAM$", totalRAM
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/NoOfVMsDeployed$", noOfVMsDeployed
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/NoOfVMsRunning$", noOfVMsRunning
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/VirtualServerVersionState$", ConvertVersionStateToString(virtualServerVersionState)
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.ManagedHost']/AgentVersionState$", ConvertVersionStateToString(agentVersionState)
End With
call oDiscoveredData.AddInstance(oHostInst)
set AddManagedHostInst = oHostInst
End Function

'=============
' Method: ConvertVersionStateToString
' Description: This function converts versionstate int to corresponding string
'=============
Function ConvertVersionStateToString(ByVal versionState)
Select Case versionState
Case 0
ConvertVersionStateToString = "Updated"
Case 1
ConvertVersionStateToString = "NeedsUpdate"
Case 2
ConvertVersionStateToString = "NotSupported"
Case Else
ConvertVersionStateToString = "MOMError"
End Select
End Function

'=============
' Method: DiscoverVMMVirtualMachine
' Description: This sub discovers Mananged VirtualMachine
'=============
Sub DiscoverVMMVirtualMachine(ByVal oDiscoveredData, ByVal cnADOConnection, ByRef oHostInst, ByVal sHostId, ByVal hostComputer, ByVal vMMComputer)
' Discover VMMVirtualMachine hosted by ManagedHost
On Error Resume Next
Dim oVirtualMachineResults, sVMAdditionsVersion, oVirtualMachineInst, oComputerInst
Set oVirtualMachineResults = cnADOConnection.Execute("select name, description, VMAddition, Owner, CreationTime, RAM, ObjectId, ProcessorCount, CostCenter, ComputerName from tbl_IL_Object where ObjectType=1 and HostID='" &amp; sHostId &amp; "'")
If Err.Number = 0 Then
Do while Not oVirtualMachineResults.EOF
IF IsEmpty(oVirtualMachineResults(2)) or IsNull(oVirtualMachineResults(2)) Then
sVMAdditionsVersion = ""
Else
sVMAdditionsVersion = oVirtualMachineResults(2)
End If
set oVirtualMachineInst = AddVirtualMachineInst(oDiscoveredData, cnADOConnection, hostComputer, vMMComputer, oVirtualMachineResults(0), oVirtualMachineResults(1), sVMAdditionsVersion, oVirtualMachineResults(3), oVirtualMachineResults(4), oVirtualMachineResults(5), oVirtualMachineResults(6), oVirtualMachineResults(7), oVirtualMachineResults(8), oVirtualMachineResults(9))

' Add HostGroupContainsManagedHost relationship
AddRelationShip oDiscoveredData, oHostInst, oVirtualMachineInst, "$MPElement[Name='Microsoft.SystemCenter.VirtualMachineManager.2007.ManagedHostContainsVirtualMachine']$"

oVirtualMachineResults.MoveNext
Loop
end if
On Error Goto 0
End Sub

'=============
' Method: AddVirtualMachineInst
' Description: This sub initializes and adds one Managed VirtualMachine
'=============
Function AddVirtualMachineInst(ByRef oDiscoveredData, ByRef aDOConnection, ByVal hostComputer, ByVal vMMComputer, ByVal vmname, ByVal description, ByVal additionsVersion, ByVal owner, ByVal creationTime, ByVal ram, ByVal vmId, ByVal noOfProcessors, ByVal costCenter, ByVal vmComputerName)
Dim oVirtualMachineInst, noOfNICs, noOfVHDs, totalSizeOfVHDs
Dim totalSizeOfVM
set oVirtualMachineInst = oDiscoveredData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.VirtualMachineManager.2007.VirtualMachine']$")
noOfNICs = NumberOfNICs(aDOConnection, vmId)
noOfVHDs = NumberOfVHDs(aDOConnection, vmId)
totalSizeOfVM = VMTotalSize(aDOConnection, vmId)
With oVirtualMachineInst
On Error Resume Next
.AddProperty "$MPElement[Name='System!System.Entity']/DisplayName$", vmname
.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", vMMComputer
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/Guid$", vmId
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/Host$", hostComputer
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/CostCenter$", costCenter
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/Name$", vmname
.AddProperty "$MPElement[Name='Virtualization!Microsoft.Virtualization.VirtualMachine']/VMAdditionsVersion$", additionsVersion
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/Description$", description
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/Owner$", owner
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/CreationTime$", creationTime
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/TotalRAM$", ram
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/NoOfNICs$", noOfNICs
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/TotalSizeOfVirtualDisks$", totalSizeOfVM
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/NoOfVirtualDisks$", noOfVHDs
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/ProcessorCount$", noOfProcessors
.AddProperty "$MPElement[Name='Virtualization!Microsoft.SystemCenter.VirtualMachineManager.VirtualMachine']/DeploymentLocation$", StrLocationHost
.AddProperty "$MPElement[Name='Virtualization!Microsoft.Virtualization.VirtualMachine']/VMComputerName$", vmComputerName
On Error Goto 0
End With
call oDiscoveredData.AddInstance(oVirtualMachineInst)
set AddVirtualMachineInst = oVirtualMachineInst
End Function

'=============
' Method: NumberOfNICs
' Description: This function queries the carmine database and returns the number of NICs a specific VM has
'=============
Function NumberOfNICs(ByRef aDOConnection, ByVal vmId)
Dim oNICs
Set oNICs = aDOConnection.Execute("select count(*) from tbl_IL_Object where ObjectType = 8 and ObjectId in ( select ChildId from tbl_IL_ObjectRelation where ParentId = '" &amp; vmId &amp; "')")
If Not IsEmpty(oNICs) Then
Do while Not oNICs.EOF
NumberOfNICs = oNICs(0)
oNICs.MoveNext
Loop
Else
NumberOfNICs = 0
End If
End Function

'=============
' Method: NumberOfVHDs
' Description: This function queries the carmine database and returns the number of VirtualDisks a specific VM has
'=============
Function NumberOfVHDs(ByRef aDOConnection, ByVal vmId)
Dim oVHDs
Set oVHDs = aDOConnection.Execute("select count(*) from tbl_IL_Object where ObjectType = 4 and ObjectId in ( select ChildId from tbl_IL_ObjectRelation where ParentId = '" &amp; vmId &amp; "')")
If Not IsEmpty(oVHDs) Then
Do while Not oVHDs.EOF
NumberOfVHDs = oVHDs(0)
oVHDs.MoveNext
Loop
Else
NumberOfVHDs = 0
End If
End Function

'=============
' Method: VMTotalSize
' Description: This function queries the carmine database and returns the total size of a VM.
' It includes all VHDs, VFDs and ISOs a specific VM has.
' ObjectType = 4 (VHD)
' ObjectType = 5 (VFD)
' ObjectType = 6 (ISO)
'=============
Function VMTotalSize(ByRef aDOConnection, ByVal vmId)
Dim totalSize
Dim oVHDVFDISO, oVHD
totalSize = 0
Set oVHDVFDISO = aDOConnection.Execute("select Sum(ObjectSize) from tbl_IL_Object where (ObjectType = 4 or ObjectType = 5 or ObjectType = 6) and ObjectId in ( select ChildId from tbl_IL_ObjectRelation where ParentId = '" &amp; vmId &amp; "')")
If Not IsEmpty(oVHDVFDISO) Then
totalSize = oVHDVFDISO(0)
If IsNull(totalSize) Then
totalSize = 0
End If
End If
totalSize = CDbl(totalSize)
' Find the leaf differencing disk VHD id
Set oVHD = aDOConnection.Execute("select ObjectId from tbl_IL_Object where ObjectType = 4 and ObjectId in ( select ChildId from tbl_IL_ObjectRelation where ParentId = '" &amp; vmId &amp; "')")
If Not IsEmpty(oVHD) Then
Do while Not oVHD.EOF
totalSize = totalSize + VHDParentSize(aDOConnection, oVHD(0))
oVHD.MoveNext
Loop
End If
VMTotalSize = totalSize
End Function

'=============
' Method: VHDParentSize
' Description: This function get the total size of a differencing disk from leaf to the root, excluding the leaf disk
'=============
Function VHDParentSize(ByRef aDOConnection, ByVal idVHD)
Dim oVHD, iVHD, oSize, tmpSize
iVHD = idVHD
oSize = CDbl(0)
Set oVHD = aDOConnection.Execute("select ObjectId, ObjectSize from tbl_IL_Object where ObjectType = 4 and ObjectId in ( select ParentId from tbl_IL_ObjectRelation where ChildId = '" &amp; iVHD &amp; "')")
Do While (Not oVHD.EOF)
iVHD = oVHD(0)
oSize = oSize + CDbl(oVHD(1))
Set oVHD = aDOConnection.Execute("select ObjectId, ObjectSize from tbl_IL_Object where ObjectType = 4 and ObjectId in ( select ParentId from tbl_IL_ObjectRelation where ChildId = '" &amp; iVHD &amp; "')")
Loop
VHDParentSize = oSize
End Function

'=============
' Method: AddRelationShip
' Description: This sub adds relationship to discovery data
'=============
Sub AddRelationShip(ByRef oDiscoveredData, ByRef oSourceInst, ByRef oTargetInst, ByVal sRelationShip)
If Not (IsNull(oSourceInst) or IsNull(oTargetInst)) Then
Dim oRelationInst
set oRelationInst = oDiscoveredData.CreateRelationshipInstance(sRelationShip)
oRelationInst.Source = oSourceInst
oRelationInst.Target = oTargetInst
call oDiscoveredData.AddInstance(oRelationInst)
End If
End Sub
</Script></ScriptBody>
<TimeoutSeconds>600</TimeoutSeconds>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
</DataSourceModuleType>