如何使用Python的PySphere获取VmWare Workstation虚拟机
更新:即使我尝试创建共享虚拟机并更改这些虚拟机的位置,问题依然存在。
我在使用 PySphere 访问位于 D:\VMs\CentOS-Backup\CentOS-Secondary-Server.vmx
的虚拟机时遇到了困难。
你能帮我一下吗?
到目前为止,我尝试过的操作有:
from pysphere import VIServer
server = VIServer()
server.connect("127.0.0.1", "my_system_login", "my_system_password")
# prints 'VMware Workstation'
print server.get_server_type()
# prints '5.1'
print server.get_api_version()
# Exception
vm = server.get_vm_by_path(r"D:\VMs\CentOS-Backup\CentOS-Secondary-Server.vmx")
vm.power_on()
提到的异常是:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7\helpers\pydev\pydevd.py", line 1473, in <module>
debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7\helpers\pydev\pydevd.py", line 1117, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "D:/Projects/PyUnitTesting/pyunittest/launcher.py", line 13, in <module>
vm = server.get_vm_by_path(r"D:\VMs\CentOS-Backup\CentOS-Secondary-Server.vmx")
File "C:\dev\Python27\lib\site-packages\pysphere\vi_server.py", line 291, in get_vm_by_path
FaultTypes.OBJECT_NOT_FOUND)
pysphere.resources.vi_exception.VIException: [Object Not Found]: Could not find a VM with path 'D:\VMs\CentOS-Backup\CentOS-Secondary-Server.vmx'
请注意,我也安装了VIX API。但没有进行任何与系统路径添加相关的具体操作。你能告诉我是否真的需要这样做吗?
谢谢!
2 个回答
1
根据文档,我觉得你不应该直接给虚拟机提供原始的文件路径。我认为有一个内部的路径值:
http://code.google.com/p/pysphere/wiki/GettingStarted#Getting_VM_properties
- get_vm_by_path:通过虚拟机的配置文件路径来获取虚拟机。要通过VMWare VI客户端获取这个值,可以按照以下步骤操作:
- 在资源树中右键点击虚拟机图标,选择“编辑设置...”。
- 切换到“选项”标签。
- 在“虚拟机配置文件”字段中可以找到这个值。
他们的示例代码是这样的:
vm1 = server.get_vm_by_path("[DataStore1] Ubuntu/Ubuntu-10.vmx")
3
我终于搞明白了!你需要确保虚拟机是共享的。可以去虚拟机的管理选项里,选择共享...
这样它们就会出现在server.get_registered_vms()这个列表里,然后你就可以用get_vm_by_name来获取它们了。