page hit counter
Welcome to MSDN Blogs Sign in | Join | Help

Taylor Brown's Blog

Test Lead for Windows Core OS Division on the Hyper-V Team.

Syndication

News

Welcome to the professional blog of Taylor Brown.  I am a test lead on the core virtualization team (Hyper-V) at Microsoft.

This blog will contain information about virtualization, Microsoft, Hyper-V, operating systems, testing, and whatever else I end up talking about...

 

Standard Microsoft Disclaimer:
"This posting is provided "AS IS" with no warranties, and confers no rights. You assume all risk for your use."

-Taylor Brown


Hyper-V WMI: What VHD’s/Physical Disks Are Associated With a Virtual Machine?

I  got a great question based on yesterday’s post (by the way my e-mail was blocking a lot of external messages that’s fixed now so I apologize to anyone I missed a mail from).  The person was asking how they could figure out what physical disk a virtual machine is using when configured for passthough on the host…  Well here’s the magic – I took it a little further and got the path’s for any attached VHD’s…  Enjoy!

 

$HyperVParent = "localhost"
$HyperVGuest = "My Virtual Machine"

$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 "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
}

 

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

clip_image001

Published Thursday, August 21, 2008 7:10 PM by taylorb

Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# send flowers » Hyper-V WMI: What VHD???s/Physical Disks Are Associated With a Virtual Machine? @ Thursday, August 21, 2008 10:25 PM

PingBack from http://hubsfunnywallpaper.cn/?p=1812

send flowers » Hyper-V WMI: What VHD???s/Physical Disks Are Associated With a Virtual Machine?

# Hyper-V WMI: What VHD’s/Physical Disks Are Associated With a Virtual Machine? - by Taylor Brown @ Friday, August 29, 2008 4:24 PM

Very handy script by Taylor Brown I got a great question based on yesterday’s post (by the way my e-mail

Ron Crumbaker at myITforum.com, Inc.

# re: Hyper-V WMI: What VHD’s/Physical Disks Are Associated With a Virtual Machine? @ Tuesday, September 09, 2008 8:40 PM

You are asking Hyper-V parent's Hyper-V WMI provider to answer which one is VHD / passthrough disk.

Without asking parent, how to detect?

 

=========================================
Without asking the parent you can't detect this information.

-Taylor

tony

# re: Hyper-V WMI: What VHD’s/Physical Disks Are Associated With a Virtual Machine? @ Thursday, November 13, 2008 1:02 PM

If anyone is trying to do this in VBScript then here's the code:

strComputer = ""

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization")

Set colSystems = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemSettingData", _

                                      "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objSystem In colSystems

  WScript.Echo "ElementName: " & objSystem.ElementName

  Set colResData = objWMIService.ExecQuery("Associators of {Msvm_VirtualSystemSettingData='" & objSystem.InstanceID & "'} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent", _

                                      "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

  For Each objResData In colResData

     If objResData.ResourceSubType = "Microsoft Virtual Hard Disk" Then

        strConnection = Join(objResData.Connection, ",")

        WScript.Echo "  Connection: " & strConnection

     End If

  Next

Next

Niall Waller

# How Are My VHD’s/Passthough Disk’s Connected To My Virtual Machine? @ Monday, February 16, 2009 8:03 PM

This is a follow up on my previous post on Hyper-V WMI: What VHD’s/Physical Disks Are Associated With

Taylor Brown's Blog

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
Page view tracker