打印虚拟机列表
Printing list of Virtual Machines
我想在 Azure 上列出我的虚拟机。此代码 运行 正确,但我正在以对象地址的形式获取输出。如何将此对象地址转换为可读信息。输出是 <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)
VM list all returns a result of Paged values。因此,为了打印所有 VM,您需要使用 by_page
或 next
.
另一个问题是 <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)
输出:
我想在 Azure 上列出我的虚拟机。此代码 运行 正确,但我正在以对象地址的形式获取输出。如何将此对象地址转换为可读信息。输出是 <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)
VM list all returns a result of Paged values。因此,为了打印所有 VM,您需要使用 by_page
或 next
.
另一个问题是 <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)
输出: