2

I want to list down my vms on my azure. This code is running correctly but the I am getting output in the form of object address. how to convert this object address to readable information. the ouput is <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)
Hassan Turi
  • 334
  • 3
  • 14
  • 1
    if it is really list then maybe you should use `for`-loop to display every element separatelly? You could also check `dir(vm_list)` to see what functions it has. Maybe it has functions which gives more details. – furas Dec 12 '21 at 18:32
  • now I have only one vm but am unable to get it. can you guide me through syntax – Hassan Turi Dec 12 '21 at 18:44
  • 2
    I have no idea what syntax is - I don't even have servers on Azure. But first what I would do is `print( dir(vm_list) )` to see all functions and properites in this object. Eventually I would use `help(vm_list)`. And if you get it with function which has name `list_all()` then I would check if this behave like real list - `for item in vm_list: print(item)`. To get more details I would also search documentation or source code of this module. – furas Dec 12 '21 at 19:32
  • 1
    If the answer was helpful, Please [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), so that others who encounter the same issue can find this solution and fix their problem. – Ansuman Bal Dec 16 '21 at 07:20

1 Answers1

1

The VM list all returns a result of Paged values. So , in order to print all the VM's you will need to use by_page or next.

Another issue is <azure.common.credentials><ServicePrincipalCredentials> doesn't have get_token , so while retrieving the paged values will error out in authentication failed. To avoid this , you can use <azure.identity><ClientSecretCredentails> which is the same as Service Principal Credentials.

I tested applying the above solutions to your code in my environment as below:

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)

Output:

enter image description here

Note: If you want to expand network_profile, diagnostics_profile etc. then you can add them in the same for loop like below:

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)

Output:

enter image description here

Ansuman Bal
  • 9,705
  • 2
  • 10
  • 27
  • I am facing this error from the your code please guide me through this error. and also your outputs are not shown the name of your vms it is showing the object address how to resolve this`File "c:\Users\USER\OneDrive\Desktop\try.py", line 19, in pageobject1 = vm_list.by_page(continuation_token=None) AttributeError: 'VirtualMachinePaged' object has no attribute 'by_page'` – Hassan Turi Dec 13 '21 at 07:34
  • 1
    @HassanTuri, lets continue in chat room : https://chat.stackoverflow.com/rooms/240087/chat-room – Ansuman Bal Dec 13 '21 at 07:37
  • 1
    @HassanTuri , please use the latest version of azure packages i.e. `azure-mgmt-compute 22.1.0` and `azure-identity 1.6.0` – Ansuman Bal Dec 13 '21 at 07:44
  • 1
    `and also your outputs are not shown the name of your vms it is showing the object address how to resolve this` my outputs are showing the VM names with other details as i have highlighted. – Ansuman Bal Dec 13 '21 at 07:46
  • Sir, I cant use the Chat room it needs 20 reputations. same error again I also updated the libraries. – Hassan Turi Dec 13 '21 at 16:48
  • @HassanTuri, was the issue resolved ? – Ansuman Bal Dec 14 '21 at 19:08