Printing list of Virtual Machines

MUBSHAR SHOUKAT 21 Reputation points
2021-12-12T18:32:58.807+00:00

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 output 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)
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,017 questions
0 comments No comments
{count} votes

Accepted answer
  1. Manu Philip 20,206 Reputation points MVP Volunteer Moderator
    2021-12-16T06:27:31.433+00:00

    azure-mgmt-resource library is not fully compatible with azure-identity. I have re-written the code as below and it works perfect
    Reference: migration_guide.md
    SP created with 'owner' permission in cli like: az ad sp create-for-rbac --name <name> --role Owner

    from azure.common.credentials import ServicePrincipalCredentials
    from azure.mgmt.compute import ComputeManagementClient
    
    credential = ServicePrincipalCredentials(client_id='your sp client id',secret='your sp secret',tenant='your tenant id')
    compute_client = ComputeManagementClient(credential, 'your subscription id')
    
    # List all Virtual Machines in the specified subscription
    def list_virtual_machines():
        for vm in compute_client.virtual_machines.list_all():
            print(vm.name)
    
    list_virtual_machines()
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2021-12-12T23:29:19.697+00:00

    Hello @MUBSHAR SHOUKAT ,
    Please add below piece of code to display the Virtual Machine Names:

    for vm in compute_client.virtual_machines.list_all():
    print("\tVM: {}".format(vm.name))

    If the above answer helps out , make sure to Upvote & Accept the Answer.

    1 person found this answer helpful.

  2. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2021-12-12T23:41:42.183+00:00

    Other simple way is:

    vms = compute_client.virtual_machines.list_all()
    for vm in vms:
    print(vm.name)

    1 person found this answer helpful.

  3. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2021-12-13T16:49:38.883+00:00

    Hello @MUBSHAR SHOUKAT ,
    It seems those are some older libraries , try use the below piece of code. I just tested it locally and working fine. You will have to install couple of packages like: pip install azure.identity to use the below code
    instead of using :
    from azure.common.credentials import ServicePrincipalCredentials
    please use:
    from azure.identity import ClientSecretCredential

    Below is the complete working code

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

    credential = ClientSecretCredential(
    tenant_id="",
    client_id="",
    client_secret=""
    )

    compute_client = ComputeManagementClient(
    credential=credential,
    subscription_id="0349d61d-b5a9-4fc0-be6e-fd9949a03e2a"
    )

    def list_virtual_machines():
    for vm in compute_client.virtual_machines.list_all():
    print(vm.name)

    list_virtual_machines()

    Please refer to the article: https://stackoverflow.com/questions/64063850/azure-python-sdk-serviceprincipalcredentials-object-has-no-attribute-get-tok

    Regards,
    Shiva.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.