Msvm_ResourceAllocationSettingData类属性连接中的Hyper-V 2012 R2 WMI错误

在Hyper-V 2008 R2和Hyper-V 2012中,我使用以下代码查找与VHD文件关联的VM名称。它工作得很好Msvm_ResourceAllocationSettingData类属性连接中的Hyper-V 2012 R2 WMI错误

功能strGetVMname(strVHD)

设置VMList = objWMIService.ExecQuery( “SELECT * FROM Msvm_ComputerSystem其中标题= '虚拟机'”)

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

端功能

这在Hyper-V 2012 R2中不再适用。我注意到WMI服务已更改为\。\ root \ virtualization \ v2。所以,我已经改变了代码

功能strGetVMname(strVHD)

设置VMList = objWMIService.ExecQuery( “SELECT * FROM Msvm_ComputerSystem其中标题= '虚拟机'”)

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

端功能

事情工作正常,除了资源类型'微软虚拟硬盘'不存在了。这是一个“微软:Hyper-V:合成磁盘驱动器”现在硬盘。 当我更改代码以反映新名称时,我无法从Instance.Connection(0)中获取对象。 类型不匹配码错误代码800A000D

任何人都可以帮助我得到这个脚本在2012 R2的工作?或PowerShell脚本中的任何帮助

回答:

2012R2 VHD文件路径位于Msvm_StorageAllocationSettingData.HostResource字段中。 这是只有第一个元素有效的字符串数组 - Msvm_StorageAllocationSettingData。

所以你基本上需要找到相应的Msvm_StorageAllocationSettingData instacnce和 从HostResource数组中提取第一个字符串。 我不擅长VB或PowerShell,所以不能给你任何有用的代码。

以上是 Msvm_ResourceAllocationSettingData类属性连接中的Hyper-V 2012 R2 WMI错误 的全部内容, 来源链接: utcz.com/qa/266744.html

回到顶部