I am using following script to get list of disks attached to vm.
$HyperVParent = "localhost"$HyperVGuest = "testvm"
$VMManagementService = Get-WmiObject -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization"
$Vm = Get-WmiObject -Namespace "root\virtualization\v2" -ComputerName $HyperVParent -Query "Select * From Msvm_ComputerSystem Where ElementName='$HyperVGuest'"
$VMSettingData = Get-WmiObject -Namespace "root\virtualization\v2" -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 "Virtual Hard Disk At: $_.Connection[0]"
}
#
#Getting the Disk Attachments for Passthrough Disks
$PhysicalDiskResource = Get-WmiObject -Namespace "root\virtualization" `
-Query "Associators of {$VMSettingData} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent" `
-ComputerName $HyperVParent | Where-Object { $_.ResourceSubType -match "Microsoft Physical Disk Drive" }
ForEach-Object -InputObject $PhysicalDiskResource -Process {
Write-Host "Passthrough Disk At: " ([WMI]$_.HostResource[0]).ElementName
Write-Host "Passthrough Disk Drive Number: " ([WMI]$_.HostResource[0]).DriveNumber
}
Same query would not work on Windows 2012 R2.
Aparently root\virtualization namespace is deprecated from the Windows 2012 R2. Did anyone observed the same?