解决方案:
首先,请注意选择用于检索虚拟设置数据的对象。为了避免在 de vms 之间有管理器,最好这样开始:
vms = client.query("select * from Msvm_ComputerSystem where Caption=\"Virtual Machine\"")
从那里我们可以迭代每个虚拟机,询问其设置,然后使用管理器检索摘要信息:
# SummaryInformation attributes
vm_attrs = {
'Name': 0,
'ElementName': 1,
'CreationTime': 2,
'Notes': 3,
'NumberOfProcessors': 4,
'ThumbnailImage': 5,
'ThumbnailImage': 6,
'ThumbnailImage': 7,
'AllocatedGPU': 8,
'EnabledState': 100,
'ProcessorLoad': 101,
'ProcessorLoadHistory': 102,
'MemoryUsage': 103,
'Heartbeat': 104,
'UpTime': 105,
'GuestOperatingSystem': 106,
'Snapshots': 107,
'AsynchronousTasks': 108,
'HealthState': 109,
'OperationalStatus': 110,
'StatusDescriptions': 111,
'MemoryAvailable': 112,
'AvailableMemoryBuffer': 113
}
vms = client.query("select * from Msvm_ComputerSystem where Caption=\"Virtual Machine\"")
management_service = client.Msvm_VirtualSystemManagementService()[0]
for vm in vms:
settings = vm.associators(wmi_result_class='Msvm_VirtualSystemSettingData')
vm_original = filter(lambda x: 'Realized' in x.VirtualSystemType, settings)
vm_snapshots = filter(lambda x: 'Snapshot' in x.VirtualSystemType, settings)
# skipped retrieving snapshots to make it shorter, but is exactly the same
vm_data = {'snapshots': len(vm_snapshots)}
paths = [vm_original[0].path_()]
summary_info = management_service.GetSummaryInformation(vm_attrs.values(), paths)
if summary_info[0] != 0:
raise Exception("Error retrieving information from vm '%s'" % vm.Name)
# Note, if you do dir() on the COMObject from the summary info list, you wont find any attribute from the docs, but, trust me, if you do summary[1][0].NumberOfProcessors or any other attribute, it will work.
vm_data.update({attr: getattr(summary_info[1][0], attr) for attr in vm_attrs.keys()})
vms_data[vm.Name] = vm_data
我花了很长时间才弄清楚...希望这最终会对其他人有所帮助。 :)