在VMware vSphere中启动虚拟机的API方法是使用vSphere Automation SDK
from vmware.vapi.vsphere.client import create_vsphere_client
from vmware.vapi.bindings.stub_config import StubConfiguration
from vmware.vapi.bindings.stub_factory import StubFactory
hostname = 'your_vcenter_hostname'
username = 'your_username'
password = 'your_password'
config = StubConfiguration(host=hostname, username=username, password=password)
client = create_vsphere_client(config)
vim.VirtualMachine
类启动虚拟机:vm_name = 'your_vm_name'
vm = client.vcenter.VM.get(vm_name)
if vm.runtime.powerState == 'poweredOff':
task = vm.powerOn()
client.vapi.wait_for_task(task)
print(f'{vm_name} 已启动')
else:
print(f'{vm_name} 已经在运行')
请确保将your_vcenter_hostname
、your_username
、your_password
和your_vm_name
替换为您的实际vCenter主机名、用户名、密码和虚拟机名称。
领取专属 10元无门槛券
手把手带您无忧上云