如何使用vsphere-automation-sdk-python获取VM父文件夹?

t3irkdon  于 2023-05-21  发布在  Python
关注(0)|答案(2)|浏览(121)

client.vcenter.vm.guest.Identity.get没有这样的选项(VM的父文件夹名称)。
我曾经考虑过使用pyVmomi,但总体来说它非常慢。

s5a0g9ez

s5a0g9ez1#

我现在无法测试它,但是如果你使用REST API,类似于下面的东西应该会给予你父文件夹:

from vmware.vapi.vsphere.client import create_vsphere_client

client = create_vsphere_client(server='your_vcenter_server', username='username', password='password')

vm = client.vcenter.VM.get('your_vm_id')

parent_folder = client.vcenter.Folder.get(vm.folder).name

VMware REST API Documentation

hwamh0ep

hwamh0ep2#

要使用vSphere Automation SDK for Python检索虚拟机的父文件夹,可以利用vSphere REST API。vSphere Automation SDK for Python基于vSphere REST API,提供了一种使用Python与vSphere进行交互的便捷方式。失效日期:

from com.vmware.vcenter_client import VM

# Assuming you have already authenticated and created the vcenter_client

# Get the VM object
vm_obj = VM.get(vcenter_client, vm_id)

# Get the VM's parent folder
folder_id = vm_obj.folder
folder_obj = vcenter_client.vcenter.Folder.get(folder_id)
parent_folder_name = folder_obj.name

print("Parent Folder Name:", parent_folder_name)

确保您已经安装了vsphere-automation-sdk Python包。您可以使用pip安装它:

pip install --upgrade --index-url=https://<vsphere-sdk-repo>/pip/v1/psc-3.0/sdk/ vsphere-automation-sdk-python

替换为vSphere Automation SDK存储库的URL。
API文档以了解更多详细信息:VMware vSphere Automation API-虚拟机操作。

相关问题