Tags

,


The following PS script is to query the VHD location of each VM running on the hosts listed in the C:\scripts\Hyperv_Hosts.txt file.

cls
cd “c:\Scripts”
$HyperVParents = Get-Content Hyperv_Hosts.txt

Foreach ($HyperVParent in $HyperVParents)
{
$VMManagementService = Get-WmiObject -class “Msvm_VirtualSystemManagementService” -namespace “root\virtualization” -ComputerName $HyperVParent
$VMs = Get-WmiObject -Namespace “root\virtualization” -ComputerName $HyperVParent -Query “Select * From Msvm_ComputerSystem Where Caption=’Virtual Machine'”
Foreach ($VM in $VMs)
{
$VMSettingData = Get-WmiObject -Namespace “root\virtualization” -Query “Associators of {$VM} Where ResultClass=Msvm_VirtualSystemSettingData AssocClass=Msvm_SettingsDefineState” -ComputerName $HyperVParent
$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” }
$VHD_PATH = $null
Foreach ($VHD in $VirtualDiskResource)
{
$VHD_PATH = $VHD_PATH + $VHD.Connection[0] + “,”
}
Write-Host $VM.ElementName, $VHD_PATH
}
}

Advertisements