Azure Python SDK solution to list VM names in subscription using Azure Cloud Shell
Here is a small snippet prepared to get VM list in an Azure subscription supported by Azure Python SDK. Hope, the code help you to develop similar code supports Azure Python SDK
Azure Cloud Shell has Python, PIP packages enabled by default
In-order to get the advantage of re-designed authentication mechanism, install azure-identity using PIP as below in Cloud Shell
1.pip install azure-identity
The code used for listing the VM names in the subscription is as follows:
01.from azure.common.credentials import ServicePrincipalCredentials
02. from azure.mgmt.compute import ComputeManagementClient
03. credential = ServicePrincipalCredentials(client_id='your sp client id',secret='your sp secret',tenant='your tenant id')
04. compute_client = ComputeManagementClient(credential, 'your subscription id')
05. # List all Virtual Machines in the specified subscription
06. def list_virtual_machines():
07. for vm in compute_client.virtual_machines.list_all():
08. print(vm.name)
09. list_virtual_machines()