Python – Control Hyper-V VMs with Python

hyper-vpythonwmi

im trying to write a control VMs on a HyperV Server using Python. I start with connecting to the server the HyperV server runs on:

connection = wmi.connect_server(server="servername", namespace=r"root\virtualization", user=r"username", password=r"password")
wmiServerConnection = wmi.WMI(wmi=connection)

This gives me a wmi object for this connection.

For stopping and starting a VM I can simply use:

#get the wmi object representing the VM
vmSystem = wmiServerConnection.Msvm_ComputerSystem(ElementName="VmName")
#send change request to vm
vmSystem[0].RequestStateChange(3)

But before starting a VM I want to apply a certain snapshot.
The class Msvm_VirtualSystemManagementService provides a method – ApplyVirtualSystemSnapshot/ApplyVirtualSystemSnapshotEx – for this. It needs the SnapshotSettingData as a parameter and I thought I could get that one using the GetSummaryInformation method of the same class. MSDN says this method returns a Msvm_SummaryInformation class.

I call this function like this:

#get the wmi class object
vmManagement = wmiServerConnection.Msvm_VirtualSystemManagementService()
snapshotInfo = vmManagement[0].GetSummaryInformation([1,107])

This should give me the name and the snapshot information for all VMs on the HyperV server. But all I get is list of COM Objects.

When I try to give a certain VM as parameter gotten from

vmSettings = wmiServerConnection.Msvm_VirtualSystemSettingData(ElementName="VmName")

like this

snapshotInfo = vmManagement[0].GetSummaryInformation([1,107], [vmSettings[0]])

it crashes.

My questions:

  1. Why don't I get a WMI object?

  2. The second parameter is obviously wrong. MSDN says it needs CIM_VirtualSystemSettingData REF SettingData[] as parameter. Is the WMI object the wrong one? How do I get the correct parameter?

  3. How can I retrieve the information I need from the COM object?

Or am I totally on the wrong track?

Thanks, Stefanie

Best Answer

So, I finally found the solution. It was much easier than I thought, but whatever:

1.Connect to your server and get the WMI-object:

connection = wmi.connect_server(server=serverName, namespace=r"root\virtualization", user=username, password=password)
wmiServerConnection = wmi.WMI(wmi=connection)

2.Get the system object and the management service object:

#get object representing VM
vmSystem = wmiServerConnection.Msvm_ComputerSystem(ElementName=VmName)
#get object responsible for VM
vmManagement = wmiServerConnection.Msvm_VirtualSystemManagementService()

3.Get the objects associated with the VM:

#get objects the VM contains
    vmObjects = vmSystem[0].associators(wmi_result_class="Msvm_VirtualSystemSettingData ")

4.Apply the snapshot you want:

for singleVmObject in vmObjects:    
    if(singleVmObject.SettingType == 5 and singleVmObject.ElementName == snapshotName):
        retVal = vmManagement[0].ApplyVirtualSystemSnapshotEx(vmSystem[0].path(), singleVmObject.path())

Further documentation can be found here:

http://timgolden.me.uk/python/wmi/wmi.html

http://msdn.microsoft.com/en-us/library/cc136986(v=vs.85).aspx