Updated 2/23 – Added VHD File Size and Fixed a Few Bugs.

This is a follow up on my previous post on Hyper-V WMI: What VHD’s/Physical Disks Are Associated With a Virtual Machine?.  I had been getting questions about how to better identify what disks are connected to what bus location/controller.  Specifically I have been getting a lot of questions about how back up or copy just the VHD that the guest sees as drive letter C or D etc… I wrote a revised script that gives a bit more information specifically the controller addresses and the Instance ID of the controller.  The reason that’s interesting is that you can determine that the C volume is on Disk Number 1 and that Disk Number 1 is connected to IDE Port 0/1 and on the parent you know that IDE Port 0/1 is backed by S:\vhds\foo.vhd well then you know what to backup…  So what about SCSI controller’s?  Well they are a bit more challenging but not to much – if you look at the PNP ID of the controller its VMBUS\<GUID> where the GUID is the same as the first GUID in the WMI instance id for the controller on the management OS (take a look at the screen capture below).  Hopefully this is helpful – Enjoy!

Here’s the Script:

$HyperVParent = "localhost" 
$VMManagementService = Get-WmiObject -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization" -ComputerName $HyperVParent

foreach ($vm in Get-WmiObject -Namespace root\virtualization -Query "Select * From Msvm_ComputerSystem where Description='Microsoft Virtual Machine'" -ComputerName $HyperVParent)
{    
$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" }
   
   
$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" }
       
   
if ($VirtualDiskResource -ne $null)
    {
       
Write-Host "VHD Connections: "
       
foreach ($i in $VirtualDiskResource)
        {
           
Write-Host " Virtual Hard Disk At: " ([WMI]$i).Connection[0]
           
Write-Host " Virtual Hard Disk VHD Size: " (get-item ([WMI]$i).Connection[0]).Length
           
Write-Host " Virtual Hard Disk Connected To: " ([WMI]([WMI]$i.Parent).Parent).ElementName
           
Write-Host " Controller Index: " ([WMI]([WMI]$i.Parent).Parent).Address
           
Write-Host " Controller Instance ID: " ([WMI]([WMI]$i.Parent).Parent).InstanceID
           
Write-Host " Disk Location On Controller: " ([WMI]$i.Parent).Address
           
Write-Host    
        }
    }
   
   
if ($PhysicalDiskResource -ne $null)
    {
       
Write-Host "Physical Disk Connections: "
       
foreach ($i in $PhysicalDiskResource)
        {
           
Write-Host " Passthrough Disk At:" ([WMI]$i.HostResource[0]).ElementName
           
Write-Host " Passthrough Disk Drive Number: " ([WMI]$i.HostResource[0]).DriveNumber
           
Write-Host " Virtual Hard Disk Connected To: " ([WMI]$i.Parent).ElementName
           
Write-Host " Controller Index: " ([WMI]$i.Parent).Address
           
Write-Host " Controller Instance ID: " ([WMI]$i.Parent).InstanceID
           
Write-Host " Disk Location On Controller: " ([WMI]$i).Address
           
Write-Host
        }
    }
}

 

Here’s the Output of the Script:

PS D:\> .\DiskAttachment2.ps1
VHD Connections:
  Virtual Hard Disk At: C:\Users\Public\Documents\Hyper-V\Virtual hard disks\SERVER2008-ENT-64-6001.18000.080118-1840_amd64fre_ServerEnterprise_en-us_VL.vhd
  Virtual Hard Disk Connected To:  IDE Controller 0
  Controller Index:  0
  Controller Instance ID:  Microsoft:4DA5F246-7501-49B3-AE41-B1B5B4FCF57F\83F8638B-8DCA-4152-9EDA-2CA8B33039B4\0
  Disk Location On Controller:  0

  Virtual Hard Disk At: C:\vhd\VHD on Local Storage.vhd
  Virtual Hard Disk Connected To:  IDE Controller 0
  Controller Index:  0
  Controller Instance ID:  Microsoft:4DA5F246-7501-49B3-AE41-B1B5B4FCF57F\83F8638B-8DCA-4152-9EDA-2CA8B33039B4\0
  Disk Location On Controller:  1

  Virtual Hard Disk At: S:\Vhds\VHD on LUN.vhd
  Virtual Hard Disk Connected To:  SCSI Controller
  Controller Index:
  Controller Instance ID:  Microsoft:4DA5F246-7501-49B3-AE41-B1B5B4FCF57F\B090A115-B8E6-4706-BE6C-C8ECDDC4A90B\0
  Disk Location On Controller:  1

  Virtual Hard Disk At: S:\Vhds\Disk on SCSI 2.vhd
  Virtual Hard Disk Connected To:  SCSI Controller
  Controller Index:
  Controller Instance ID:  Microsoft:4DA5F246-7501-49B3-AE41-B1B5B4FCF57F\87617569-E20C-4982-AC44-04A4251C82BA\0
  Disk Location On Controller:  0

Physical Disk Connections:
  Passthrough Disk At: Disk 3
  Passthrough Disk Drive Number:   3
  Virtual Hard Disk Connected To:  SCSI Controller
  Controller Index:
  Controller Instance ID:  Microsoft:4DA5F246-7501-49B3-AE41-B1B5B4FCF57F\B090A115-B8E6-4706-BE6C-C8ECDDC4A90B\0
  Disk Location On Controller:  0

 

Here’s a Screen Capture Of the PNP ID in the Guest:

image

Taylor Brown
Hyper-V Integration Test Lead
http://blogs.msdn.com/taylorb

clip_image001