1

In Hyper-V 2008 R2 and Hyper-V 2012 I used the following code to find the VM name associated with the VHD files. It worked fine

Function strGetVMname(strVHD)

Set VMList = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem Where Caption='Virtual Machine'")

For Each VirtualMachine in VMList
    Set vm = GetComputerSystem(VirtualMachine.ElementName)
    strQuery = "ASSOCIATORS OF {" & vm.Path_.Path & "} WHERE resultClass = Msvm_VirtualSystemSettingData"
    set virtualSystemSetting = objWMIService.ExecQuery(strQuery).ItemIndex(0)

    strQuery = Format1("ASSOCIATORS OF {{0}} WHERE " &_
                " resultClass = Msvm_ResourceAllocationSettingData" &_
                " AssocClass = Msvm_VirtualSystemSettingDataComponent " &_
                " ResultRole = PartComponent " &_
                " Role = GroupComponent", virtualSystemSetting.Path_.Path)
    set objInstances = objWMIService.ExecQuery(strQuery)

        for each Instance in objInstances
        If (Instance.ResourceSubType = "Microsoft Virtual Hard Disk") Then
            strMatch = InStr(1,Instance.Connection(0),strVHD,1)
            If (strMatch = 1) Then
                strGetVMname = VirtualMachine.ElementName
                exit for
            End If
        End If
    next
Next

End Function

This doesn't work anymore in Hyper-V 2012 R2.I've noticed the WMI service changed to \.\root\virtualization\v2. So i have changed the code

Function strGetVMname(strVHD)

Set VMList = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem Where Caption='Virtual Machine'")

For Each VirtualMachine in VMList
    Set vm = GetComputerSystem(VirtualMachine.ElementName)
    strQuery = "ASSOCIATORS OF {" & vm.Path_.Path & "} WHERE resultClass = Msvm_VirtualSystemSettingData"
    set virtualSystemSetting = objWMIService.ExecQuery(strQuery).ItemIndex(0)

    strQuery = Format1("ASSOCIATORS OF {{0}} WHERE " &_
                " resultClass = Msvm_ResourceAllocationSettingData" &_
                " AssocClass = Msvm_VirtualSystemSettingDataComponent " &_
                " ResultRole = PartComponent " &_
                " Role = GroupComponent", virtualSystemSetting.Path_.Path)
    set objInstances = objWMIService.ExecQuery(strQuery)

        for each Instance in objInstances
        If (Instance.ResourceSubType = "Microsoft:Hyper-V:Synthetic Disk Drive") Then
            strMatch = InStr(1,Instance.Connection(0),strVHD,1)
            If (strMatch = 1) Then
                strGetVMname = VirtualMachine.ElementName
                exit for

            End If


        End If
    next
Next

End Function

Thing work fine then except for the Resource type 'Microsoft Virtual Hard Disk' doesnt exists anymore. It's a "Microsoft:Hyper-V:Synthetic Disk Drive" harddrive now. When I change the code to reflect to the new name I cannot get an object anymore from Instance.Connection(0). getting error at same line for Type Mismatch Code 800A000D

Can anyone help me to get this script working in 2012 R2? or Any help in powershell script

1 Answer 1

3

In 2012R2 VHD file path is located in Msvm_StorageAllocationSettingData.HostResource field. It's an array of strings where only first element is valid - Msvm_StorageAllocationSettingData.

So you basically need to find corresponding Msvm_StorageAllocationSettingData instacnce and extract first string from HostResource array. I'm not good with VB or PowerShell so can't give you any useful code.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.