本文介绍了使用Python控制Hyper-V VM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im试图使用Python在HyperV Server上编写控制VM.我首先连接到运行HyperV服务器的服务器:

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)

这为该连接提供了一个wmi对象.

This gives me a wmi object for this connection.

要停止和启动VM,我可以简单地使用:

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)

但是在启动VM之前,我想应用某个快照.Msvm_VirtualSystemManagementService类为此提供了一种方法-ApplyVirtualSystemSnapshot/ApplyVirtualSystemSnapshotEx.它需要SnapshotSettingData作为参数,我想我可以使用同一类的GetSummaryInformation方法获得该参数. MSDN表示,此方法返回 Msvm_SummaryInformation 课.

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.

我这样称呼这个函数:

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

这应该为我提供HyperV服务器上所有VM的名称和快照信息.但是我得到的只是COM对象列表.

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")

像这样

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

它崩溃了.

我的问题:

  1. 为什么我没有WMI对象?

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

第二个参数显然是错误的. MSDN表示需要CIM_VirtualSystemSettingData REF SettingData[]作为参数. WMI对象是错误的对象吗?如何获取正确的参数?

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?

如何从COM对象中检索所需的信息?

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

还是我完全走错了路?

谢谢,孙燕姿

推荐答案

因此,我终于找到了解决方案.这比我想象的要容易得多,但是无论如何:

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

1.连接到服务器并获取WMI对象:

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.获取系统对象和管理服务对象:

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.获取与VM关联的对象:

3.Get the objects associated with the VM:

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

4.应用所需的快照:

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

这篇关于使用Python控制Hyper-V VM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:24