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_s
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