Control Hyper-V VMs with Python

后端 未结 1 1332
甜味超标
甜味超标 2020-12-17 07:35

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         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 08:04

    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

    0 讨论(0)
提交回复
热议问题