none
Hyper-V WMI provider (V2) - Msvm_ResourceAllocationSettingData class RRS feed

  • Question

  • I have an application that queries the Hyper-V WMI classes for all the virtual hard disks associated with a virtual machine.  There are many samples out there on how to do this.  One of which is http://blogs.msdn.com/b/virtual_pc_guy/archive/2010/08/10/find-all-virtual-hard-disks-associated-with-a-virtual-machine-script.aspx

    The issue that I am encountering is around the "Connection" property in the Msvm_ResourceAllocationSettingData class.  When connecting to root\virtualization, the connection property of the Msvm_ResourceAllocationSettingData class is set to the path of the vhd.  But when connecting to root\virtualization\v2, the connection property is null.

     

    Below is the sample code that I ran on a Windows Server 2012 system.

    using System;
    using System.Linq;
    using System.Management;
    
    namespace GetVHD
    {
        class Program
        {
            const string Server = "nick-w2k8-srv3";
            const string Username = "nick";
            const string Password = "***";
            const string Vmname = "nick-opensuse";
    
            static ManagementObjectCollection GetResourceSettings(ManagementScope scope)
            {
                var query = new SelectQuery("Select * from Msvm_ComputerSystem where ElementName='" + Vmname + "'");
                var searcher = new ManagementObjectSearcher(scope, query);
                ManagementObjectCollection vmCollection = searcher.Get();
    
                if (vmCollection.Count != 1)
                {
                    var message = String.Format("VM {0} is not valid", Vmname);
                    throw new Exception(message);
                }
    
                ManagementObject vm = vmCollection.Cast<ManagementObject>().First();
    
                ManagementObjectCollection virtualSystemSettingsCollection = vm.GetRelated("Msvm_VirtualSystemSettingData",
                                                                                           "Msvm_SettingsDefineState",
                                                                                           null, null, null, null, false, null);
                if (virtualSystemSettingsCollection.Count != 1)
                {
                    throw new Exception("Msvm_VirtualSystemSettingData is not valid");
                }
    
                ManagementObject virtualSystemSettings = virtualSystemSettingsCollection.Cast<ManagementObject>().First();
    
                var resourceAllocationSettingsCollection =
                    virtualSystemSettings.GetRelated("Msvm_ResourceAllocationSettingData",
                                                     "Msvm_VirtualSystemSettingDataComponent",
                                                     null, null, null, null, false, null);
    
                return resourceAllocationSettingsCollection;
            }
    
            static void ShowAllVhdsFromVirtualization()
            {
                System.Console.WriteLine("Connecting to root\\virtualization");
    
                var options = new ConnectionOptions
                {
                    Username = Username,
                    Password = Password,
                    Impersonation = ImpersonationLevel.Impersonate,
                    Authentication = AuthenticationLevel.PacketPrivacy
                };
    
                const string serverString = @"\\" + Server + @"\root\virtualization";
                var scope = new ManagementScope(serverString, options);
                scope.Connect();
    
                var resourceAllocationSettingsCollection = GetResourceSettings(scope);
    
                foreach (var resourceAllocationSetting in resourceAllocationSettingsCollection)
                {
                    var resourceSubType = resourceAllocationSetting["ResourceSubType"] as string;
                    if (resourceSubType != null && String.CompareOrdinal("Microsoft Virtual Hard Disk", resourceSubType) == 0)
                    {
                        System.Console.WriteLine("Found Microsoft Virtual Hard Disk");
                        var connectionArray = resourceAllocationSetting["Connection"] as string[];
                        var connection = (connectionArray == null) ? "" : connectionArray[0];
                        System.Console.WriteLine("Connection: {0}", connection ?? "");
                    }
                }
            }
    
            static void ShowAllVhdsFromVirtualizationV2()
            {
                System.Console.WriteLine("Connecting to root\\virtualization\\v2");
    
                var options = new ConnectionOptions
                {
                    Username = Username,
                    Password = Password,
                    Impersonation = ImpersonationLevel.Impersonate,
                    Authentication = AuthenticationLevel.PacketPrivacy
                };
    
                const string serverString = @"\\" + Server + @"\root\virtualization\v2";
                var scope = new ManagementScope(serverString, options);
                scope.Connect();
    
                var resourceAllocationSettingsCollection = GetResourceSettings(scope);
    
                foreach (var resourceAllocationSetting in resourceAllocationSettingsCollection)
                {
                    var resourceSubType = resourceAllocationSetting["ResourceSubType"] as string;
                    if (resourceSubType != null && String.CompareOrdinal("Microsoft:Hyper-V:Synthetic Disk Drive", resourceSubType) == 0)
                    {
                        System.Console.WriteLine("Found Microsoft:Hyper-V:Synthetic Disk Drive");
                        var connectionArray = resourceAllocationSetting["Connection"] as string[];
                        var connection = (connectionArray == null) ? "" : connectionArray[0];
                        System.Console.WriteLine("Connection: {0}", connection ?? "");
                    }
                }
            }
    
            static void Main(string[] args)
            {
                ShowAllVhdsFromVirtualization();
                ShowAllVhdsFromVirtualizationV2();
            }
        }
    }
    

    Below is the output.

    Connecting to root\virtualization
    Found Microsoft Virtual Hard Disk
    Connection: C:\Users\Public\Documents\Hyper-V\Virtual hard disks\Nicks_openSUSE_13.1_JeOS.i686-0.0.2.vhd
    Connecting to root\virtualization\v2
    Found Microsoft:Hyper-V:Synthetic Disk Drive
    Connection:

     

    Any help is highly appreciated.

     

    Thanks,

    Nick

                             
    Tuesday, May 20, 2014 4:32 PM

Answers

  • I looked into this a bit more and it seems like there a "new" way to get the list of vhds for a given virtual machine by using the HostResource property in the Msvm_StorageAllocationSettingData class.

    using System;
    using System.Linq;
    using System.Management;
    
    namespace GetVHD
    {
        class Program
        {
            const string Server = "nick-w2k8-srv3";
            const string Username = "nick";
            const string Password = "*";
            const string Vmname = "nick-opensuse";
    
            static void Main(string[] args)
            {
                var options = new ConnectionOptions
                {
                    Username = Username,
                    Password = Password,
                    Impersonation = ImpersonationLevel.Impersonate,
                    Authentication = AuthenticationLevel.PacketPrivacy
                };
    
                const string serverString = @"\\" + Server + @"\root\virtualization\v2";
                var scope = new ManagementScope(serverString, options);
                scope.Connect();
    
                var query = new SelectQuery("Select * from Msvm_ComputerSystem where ElementName='" + Vmname + "'");
                var searcher = new ManagementObjectSearcher(scope, query);
                ManagementObjectCollection vmCollection = searcher.Get();
    
                if (vmCollection.Count != 1)
                {
                    var message = String.Format("VM {0} is not valid", Vmname);
                    throw new Exception(message);
                }
    
                ManagementObject vm = vmCollection.Cast<ManagementObject>().First();
    
                ManagementObjectCollection virtualSystemSettingsCollection = vm.GetRelated("Msvm_VirtualSystemSettingData",
                                                                                           "Msvm_SettingsDefineState",
                                                                                           null, null, null, null, false, null);
                if (virtualSystemSettingsCollection.Count != 1)
                {
                    throw new Exception("Msvm_VirtualSystemSettingData is not valid");
                }
    
                ManagementObject virtualSystemSettings = virtualSystemSettingsCollection.Cast<ManagementObject>().First();
    
                const UInt16 SASDResourceTypeLogicalDisk = 31;
    
    
                ManagementObjectCollection storageAllocationSettingCollection =
                    virtualSystemSettings.GetRelated("Msvm_StorageAllocationSettingData",
                                                     "Msvm_VirtualSystemSettingDataComponent",
                                                     null, null, null, null, false, null);
    
                foreach (ManagementObject storageAllocationSetting in storageAllocationSettingCollection)
                {
                    if ((UInt16) storageAllocationSetting["ResourceType"] == SASDResourceTypeLogicalDisk)
                    {
                        var hostResourceArray = storageAllocationSetting["HostResource"] as string[];
                        var hostResource = (hostResourceArray == null) ? "" : hostResourceArray[0];
                        System.Console.WriteLine("HostResource: {0}", hostResource ?? "");
                    }
                }
            }
        }
    }
    

    • Marked as answer by NickDala Tuesday, May 20, 2014 6:31 PM
    Tuesday, May 20, 2014 6:31 PM

All replies

  • I looked into this a bit more and it seems like there a "new" way to get the list of vhds for a given virtual machine by using the HostResource property in the Msvm_StorageAllocationSettingData class.

    using System;
    using System.Linq;
    using System.Management;
    
    namespace GetVHD
    {
        class Program
        {
            const string Server = "nick-w2k8-srv3";
            const string Username = "nick";
            const string Password = "*";
            const string Vmname = "nick-opensuse";
    
            static void Main(string[] args)
            {
                var options = new ConnectionOptions
                {
                    Username = Username,
                    Password = Password,
                    Impersonation = ImpersonationLevel.Impersonate,
                    Authentication = AuthenticationLevel.PacketPrivacy
                };
    
                const string serverString = @"\\" + Server + @"\root\virtualization\v2";
                var scope = new ManagementScope(serverString, options);
                scope.Connect();
    
                var query = new SelectQuery("Select * from Msvm_ComputerSystem where ElementName='" + Vmname + "'");
                var searcher = new ManagementObjectSearcher(scope, query);
                ManagementObjectCollection vmCollection = searcher.Get();
    
                if (vmCollection.Count != 1)
                {
                    var message = String.Format("VM {0} is not valid", Vmname);
                    throw new Exception(message);
                }
    
                ManagementObject vm = vmCollection.Cast<ManagementObject>().First();
    
                ManagementObjectCollection virtualSystemSettingsCollection = vm.GetRelated("Msvm_VirtualSystemSettingData",
                                                                                           "Msvm_SettingsDefineState",
                                                                                           null, null, null, null, false, null);
                if (virtualSystemSettingsCollection.Count != 1)
                {
                    throw new Exception("Msvm_VirtualSystemSettingData is not valid");
                }
    
                ManagementObject virtualSystemSettings = virtualSystemSettingsCollection.Cast<ManagementObject>().First();
    
                const UInt16 SASDResourceTypeLogicalDisk = 31;
    
    
                ManagementObjectCollection storageAllocationSettingCollection =
                    virtualSystemSettings.GetRelated("Msvm_StorageAllocationSettingData",
                                                     "Msvm_VirtualSystemSettingDataComponent",
                                                     null, null, null, null, false, null);
    
                foreach (ManagementObject storageAllocationSetting in storageAllocationSettingCollection)
                {
                    if ((UInt16) storageAllocationSetting["ResourceType"] == SASDResourceTypeLogicalDisk)
                    {
                        var hostResourceArray = storageAllocationSetting["HostResource"] as string[];
                        var hostResource = (hostResourceArray == null) ? "" : hostResourceArray[0];
                        System.Console.WriteLine("HostResource: {0}", hostResource ?? "");
                    }
                }
            }
        }
    }
    

    • Marked as answer by NickDala Tuesday, May 20, 2014 6:31 PM
    Tuesday, May 20, 2014 6:31 PM
  • Dear I'm newbie on VMI & Hyper-V,

    so i would like to know if it's possible to get also Max Size and Allocation size for each  virtual hard disks associated with a virtual machine.

    Thanks

    O.


    Wednesday, July 26, 2017 3:45 PM
  • Hi Nick,

    Thanks for this question and answer. I could've spent about 2 months trying to figure this out using the connection property which is always null.

    Thanks

    Wednesday, March 20, 2019 1:55 PM