我试图从Hyper-V服务器上的所有可用虚拟机中检索信息。问题是,当我要求摘要信息时,我得到一个无用的COMObjects列表。
我找不到获取实际SummaryInformation值的方法..
以下是代码:
import wmi
conn = wmi.connect_server(server="xxxx", user="xxxx", password="xxx", namespace=r"root\virtualization\v2")
client = wmi.WMI(wmi=conn)
mgs = client.Msvm_VirtualSystemManagementService()
summaries = mgs[0].GetSummaryInformation()
print summaries
# (0, [<COMObject <unknown>>, <COMObject <unknown>>, <COMObject <unknown>>])
所以我尝试检索一个VirtualSystemData作为参数传递给getSummary
vs = h.Msvm_VirtualSystemSettingData()
vs[0]
#Out[34]: <_wmi_object: \\WIN-Lxxxxxx\root\virtualization#\v2:Msvm_VirtualSystemSettingData.InstanceID="Microsoft:xxx-xxx-xxx-xxx">
mgs[0].GetSummaryInformation([vs[0].ole_object])
#Out[38]: (0, [<COMObject <unknown>>, <COMObject <unknown>>, <COMObject <unknown>>])
有什么想法吗?
答案 0 :(得分:1)
<强>解决方案:强>
首先,请注意选择的对象以检索虚拟设置数据。为了避免在de vms之间拥有经理,最好这样开始:
vms = client.query("select * from Msvm_ComputerSystem where Caption=\"Virtual Machine\"")
从那里我们可以迭代每个vm,询问它的设置,然后使用管理器来检索摘要信息:
# 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
我花了很长时间才想出来......希望这最终会帮助别人。 :)