Unable to discover Virtual hard disk in windows server 2016 through WMI query


  • I have following setup
    windows 2016 server with Hyper-V and there is one VM (generation1)

    To collect the data for virtual hard disk attached to this vm i was using the following code

    ObjectQuery wqlQuery = new ObjectQuery("Select * from MSVM_ComputerSystem where NOT ElementName like \'" + name + "\'");
    ManagementObjectSearcher querySearcher = new ManagementObjectSearcher(_conData.virtMgmtScope, wqlQuery);
    // Get the results.
    ManagementObjectCollection vmCollection = querySearcher.Get();
    // Loop for every VM.
    foreach (ManagementObject oReturn in vmCollection)
    	List<Resource> resourceList = new List<Resource>();
    	string hostName = obj["ElementName"].ToString();
    	ObjectQuery wqlQuery = new System.Management.ObjectQuery("Associators of {" + obj + "} Where ResultClass=Msvm_VirtualSystemSettingData AssocClass=Msvm_SettingsDefineState");
    	ManagementObjectSearcher querySearcher = new ManagementObjectSearcher(_conData.virtMgmtScope, wqlQuery);
    	ManagementObjectCollection virtualSystemCollection = querySearcher.Get();
    	foreach (ManagementObject virtualSystemCollectionObj in virtualSystemCollection)
    		wqlQuery = new System.Management.ObjectQuery("Associators of {" + virtualSystemCollectionObj + "} Where ResultClass=" + Msvm_ResourceAllocationSettingData + " AssocClass=Msvm_VirtualSystemSettingDataComponent");
    		querySearcher = new ManagementObjectSearcher(_conData.virtMgmtScope, wqlQuery);
    		ManagementObjectCollection resourceAlloactionCollection = querySearcher.Get();
    		foreach (ManagementObject resourceAlloactionObject in resourceAlloactionCollection)
    		 if ((resourceAlloactionObject["ResourceSubType"] != null) &&
    			(resourceAlloactionObject["ResourceSubType"].ToString().Contains("Virtual Hard Disk")))
    				//some task

    I was not getting any output resourceAlloactionObject with ResourceSubType as "Virtual Hard Disk".

    I cross checked this on WMI explorer, still dint found any.

    Can any help me out, what possibly i am missing here.

    Wednesday, February 22, 2017 9:45 AM

All replies

  • Hi Sir,

    I only tried to get that information with powershell .

    Please try the following powershell script (please run it as administrator) :

    $HyperVParent = "localhost" 
    $HyperVGuest = "elton-sql" 
    $VMManagementService = Get-WmiObject -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization\v2" -ComputerName $HyperVParent 
    $Vm = Get-WmiObject -Namespace "root\virtualization\v2" -ComputerName $HyperVParent -Query "Select * From Msvm_ComputerSystem Where ElementName='$HyperVGuest'" 
    $VMSettingData = Get-WmiObject -Namespace "root\virtualization\v2" -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\v2" -Query "Associators of {$VMSettingData} Where ResultClass=Msvm_StorageAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent" -ComputerName $HyperVParent 

    To get further assistance , I'd suggest you to post this question into MSDN froum:


    Best Regards,


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Friday, February 24, 2017 5:36 AM