2

我想在我的天蓝色上列出我的虚拟机。此代码运行正常,但我以对象地址的形式获得输出。如何将此对象地址转换为可读信息。输出是<azure.mgmt.compute.v2019_03_01.models.virtual_machine_paged.VirtualMachinePaged object at 0x00000200D5C49E50>

from azure.mgmt.compute import ComputeManagementClient
from azure.common.credentials import ServicePrincipalCredentials


Subscription_Id = "XXXXXX"
Tenant_Id = "XXXX"
Client_Id = "XXXX"
Secret = "XXXXX"

credential = ServicePrincipalCredentials(
    client_id=Client_Id,
    secret=Secret,
    tenant=Tenant_Id
)

compute_client = ComputeManagementClient(credential, Subscription_Id)

vm_list = compute_client.virtual_machines.list_all()
print(vm_list)
4

1 回答 1

1

VM 列表全部返回分页值的结果。因此,为了打印您需要使用的所有 VMby_pagenext.

另一个问题是<azure.common.credentials><ServicePrincipalCredentials>没有get_token,因此在检索分页值时将在身份验证失败时出错。为避免这种情况,您可以使用<azure.identity><ClientSecretCredentails>与服务主体凭据相同的凭据。

我在我的环境中测试了将上述解决方案应用于您的代码,如下所示:

from azure.mgmt.compute import ComputeManagementClient
from azure.identity import ClientSecretCredential


Subscription_Id = "xxxx"
Tenant_Id = "xxxx"
Client_Id = "xxxxx"
Secret = "xxxxx"

credential = ClientSecretCredential(
    client_id=Client_Id,
    client_secret=Secret,
    tenant_id=Tenant_Id
)

compute_client = ComputeManagementClient(credential, Subscription_Id)

vm_list = compute_client.virtual_machines.list_all()
pageobject1=vm_list.by_page(continuation_token=None)
for page in pageobject1:
    for j in page:
        print(j)

输出:

在此处输入图像描述

注意: 如果要扩展network_profile等,diagnostics_profile则可以将它们添加到相同的 for 循环中,如下所示:

vm_list = compute_client.virtual_machines.list_all()

pageobject1=vm_list.by_page(continuation_token=None)
for page in pageobject1:
    for j in page:
        network_profile=j.network_profile
        hardware_profile=j.hardware_profile
        print("VM Details : ", j)
        print("Network Profile : ", network_profile)
        print("Hardware Profile : " , hardware_profile)

输出:

在此处输入图像描述

于 2021-12-13T06:07:56.347 回答