none
For each VM how to get the path (in the parent machine) for the VHD file? RRS feed

  • Question

  • I have used the query ("SELECT * FROM Msvm_ComputerSystem) to enumerate VMs. I am trying to get the paths to the VHD file(s) and the VM definition files. VM files are typically in "C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines". How to get this information using the Hyper V WMI classes?

    Thanks,
    csundaresh


    Friday, May 2, 2008 5:17 PM

All replies

  • HI,
      I also tried doing the same (in C++ and powershell).
    Below link will give useful information.
    http://dungkhoang.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&_c=BlogPart&partqs=amonth%3d5%26ayear%3d2008

    However the powershell script given in the link might not run. One double quote is missing there. After fixing that if you don't get any useful output try giving 'incorrect' VM name then it gives information of all the VMs. If you have a working C++ exmple inform me too.

    thanks,
    Madhu

    Thursday, May 8, 2008 11:41 AM
  • Hi all,

    To find the default path where Hyper-V stores VHD files and snapshots, you use the class Msvm_VirtualSystemGlobalSettingData and check the 2 properties: ExternalDataRoot and SnapshotDataRoot.

     

    /Dung

     

    PS: Madhu,

    Thanks for pointing out the error. I updated my blog entery and the script is working now

     

     

    Wednesday, May 14, 2008 12:22 AM
  • Here's the code in VBScript:

     

    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

    • Proposed as answer by NIall Waller Friday, March 4, 2011 12:44 PM
    Thursday, November 13, 2008 6:04 PM