I've found the following Powershell script to extract information from Hyper-V machines. I'm trying to convert it to VB.NET but I'm having trouble figuring out the WQL syntax
This is what I have so far but, naturally, it doesn't like me trying to append the VM object as a string in the query. I guess Powershell handles this object in the WMI query slightly differently.
Code:
$HyperVParent = "localhost"
$HyperVGuest = myserver"
$VMManagementService = Get-WmiObject -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization" -ComputerName $HyperVParent
$Vm = Get-WmiObject -Namespace "root\virtualization" -ComputerName $HyperVParent -Query "Select * From Msvm_ComputerSystem Where ElementName='$HyperVGuest'"
$VMSettingData = Get-WmiObject -Namespace "root\virtualization" -Query "Associators of {$Vm} Where ResultClass=Msvm_VirtualSystemSettingData AssocClass=Msvm_SettingsDefineState" -ComputerName $HyperVParent
#
#Getting the Path's for Attached VHD's
$VirtualDiskResource = Get-WmiObject -Namespace "root\virtualization" -Query "Associators of {$VMSettingData} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent" -ComputerName $HyperVParent | Where-Object { $_.ResourceSubType -match "Microsoft Virtual Hard Disk" }
ForEach-Object -InputObject $VirtualDiskResource -Process {
Write-Host $_.Connection[0]
}
Code:
Dim vm As ManagementObject = GetWMIObjectQuery(String.Format("Select * from msvm_computersystem where elementname='{0}'", strcomp))
Dim VMSettingsObj As ManagementObject = GetWMIObjectQuery("Associators of {" + vm + "} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_ShutdownComponent")
Private Shared Function GetWMIObjectQuery(query As String) As ManagementObject
Dim moResult As ManagementObject = Nothing
Dim oSearcher As New ManagementObjectSearcher(String.Format("\\{0}\root\virtualization"), query)
Dim oResults As ManagementObjectCollection = oSearcher.[Get]()
If oResults.Count = 1 Then
For Each mo As ManagementObject In oResults
moResult = mo
Next
End If
If moResult Is Nothing Then
Throw New Exception(String.Format("Query string found no management object - Query: {0}", query))
End If
Return moResult
End Function