Python WMI Hyper-v GetSummaryInformation result

2024/10/15 11:29:11

Im trying to retrieve information from all the available VMs on a Hyper-V Server. The problem is that when I ask for the summary information, i get a list of useless COMObjects.

I can't find a way of getting the actual SummaryInformation values..

Here's the code:

import wmiconn = 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>>])

So I tried retrieving one VirtualSystemData to pass as parameter to 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>>])

Any ideas?

Answer

Solution:

First, be careful about the objects selected to retrieve the virtual settings data. To avoid having the manager between de vms, is better to start like this:

vms = client.query("select * from Msvm_ComputerSystem where Caption=\"Virtual Machine\"")

From there we can iterate each vm, ask for its settings, and then use the manager to retrieve the summary information:

# 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

It took me quite a while to figure it out... hope this will help someone else eventually. :)

https://en.xdnf.cn/q/117841.html

Related Q&A

How to dynamically change variable name in form.vars.var_name

I have defined counter variable in controller.I can define tables and fields dynamically.tables = [db.define_table(example_table_%s % x,Field(example_field_%s % x, type=string, ...)...)for x in range(0…

Why will one loop modify a list of lists, but the other wont [duplicate]

This question already has answers here:Python list doesnt reflect variable change(6 answers)Closed 8 years ago.One of the answers in "python way" to parse and conditionally replace every elem…

Python CGI executable script downloads / shows script code

A script I wrote long ago (getWords.py) used to be executable at my localhost (http://local.example.com/getWords.py?query-string)My python script starts like this :#!/usr/bin/env python # chmod 755 ge…

My function returns None

I am new to Python and I was trying to solve this exercise, but keep getting None output. The question asked for a program in which the input is hours and rate and the output is the gross pay, includin…

Django undefined symbol: PyUnicode_AsUTF8

I am new to Python/Django. I have set up the environment needed to run Django project.When Im trying to migrate an existing project , it shows up this errordjango.core.exceptions.ImproperlyConfigured: …

using a variable keyword for an optional argument name with python argparse

I am using argparse for a python script I am writing. The purpose of the script is to process a large ascii file storing tabular data. The script just provides a convenient front-end for a class I have…

Error while setting up MongoDB with django using django mongodb engine on windows

Steps I followed :pip install git+htp://github.com/django-nonrel/[email protected]It did not work, so I downloaded the zip from the site "htp://github.com/django-nonrel/django" and pasted the…

Python login page with pop up windows

I want to access webpages and print the source codes with python, most of them require login at first place. I have similar problem before and I have solved it with the following code, because they are…

Calculate Scipy LOGNORM.CDF() and get the same answer as MS Excel LOGNORM.DIST

I am reproducing a chart in a paper using the LOGNORM.DIST in Microsoft Excel 2013 and would like to get the same chart in Python. I am getting the correct answer in excel, but not in python.In excel …

Python MySQLdb cursor.execute() insert with varying number of values

Similar questions have been asked, but all of them - for example This One deals only with specified number of values.for example, I tried to do this the following way:def insert_values(table, columns, …