Nadav
Nadav

Reputation: 2727

Azure python ComputeManagementClient retrieve number of machines for virtual machine scale sets (per date)

I am trying to retrieve the number of machines for virtual machine scale sets in python

Edit: something that I failed to mention is that I need this data per date

wanted result:

Thanks

Upvotes: 0

Views: 245

Answers (1)

Jim Xu
Jim Xu

Reputation: 23141

If you want to list the vm in Azure virtual machine scale sets in python application, please refer to the following steps

  1. Create a service principal and assign Contributor role to the sp
az login
#create sp and assign Contributor role at subscription level
az ad sp create-for-rbac -n "your service principal name"

enter image description here

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

client_id = "sp appId"
secret = "sp password"
tenant = "sp tenant"
credentials = ServicePrincipalCredentials(
        client_id = client_id,
        secret = secret,
        tenant = tenant
)

Subscription_Id = ''
compute_client =ComputeManagementClient(credentials,Subscription_Id)
resource_group_name='Networking-WebApp-AppGW-V1-E2ESSL'
virtual_machine_scale_set_name='VMSS'
result=compute_client.virtual_machine_scale_set_vms.list(resource_group_name,virtual_machine_scale_set_name)
num=0
for item in result:
     num+=1
     print(item.name)

print(f'the number of vm in the virtual machine scale sets is {num}')

enter image description here

For more details, please refer to here.

Upvotes: 1

Related Questions