因此,我尝试使用 pyvmomi 制作一个 Python 脚本来控制在 ESXi 服务器上运行的虚拟机的状态。基本上,我尝试使用 connection.content.searchIndex.FindByIp(ip="the ip of the VM", vmSearch=True)
来获取我的虚拟机,然后打开它的电源,但我当然无法获取VM 关闭时的 IP。所以,我想知道是否有任何方法可以通过名称或 ID 获取虚拟机?我进行了相当多的搜索,但找不到真正的解决方案。不管怎样,到目前为止,这是我的代码:
from pyVim import connect
# Connect to ESXi host
connection = connect.Connect("192.168.182.130", 443, "root", "password")
# Get a searchIndex object
searcher = connection.content.searchIndex
# Find a VM
vm = searcher.FindByIp(ip="192.168.182.134", vmSearch=True)
# Print out vm name
print (vm.config.name)
# Disconnect from cluster or host
connect.Disconnect(connection)
最佳答案
searchindex 没有任何方法来执行“findbyname”,因此您可能不得不撤回所有虚拟机并在客户端进行过滤。
以下是返回所有虚拟机的示例:https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/getallvms.py
另一个选择是,如果您使用的是 vCenter 6.5+,则可以使用适用于 Python 的 vSphere Automation SDK,您可以在其中与 REST API 交互以执行服务器端筛选。更多信息:https://github.com/vmware/vsphere-automation-sdk-python
关于python-3.x - 使用 pyvmomi 从关闭状态启动 VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52581228/