Get VM status using Azure Python SDK

2019-08-14 04:54发布

问题:

I have a list of VMs and I'd like to get each VM's status (ReadyRole/Stopped/StoppedDeallocated) using Azure's Python SDK.

I have done this in a bash terminal using azure cli commands and a combination of grep,tail and such utils but I'd like to do that in python script using Azure's SDK.

With the help of azure cli I run in a shell script azure vm list and then grep my way to get the status of the VMs.

I've been looking into servicemanagementservice.py of Azure SDK but I can't find a function like get_role_status(). list_hosted_services() and get_hosted_service_properties don't seem to provide the info I want, unless I'm missing something.

Can anyone point me towards a solution?

回答1:

Base on my experience, we can get every instances status using Azure REST API. So Azure SDK for python should have similar method, because the functions in Azure SDK use the same URL as REST API.
I tried to use this method get_deployment_by_name to get the instances status:

subscription_id = '****-***-***-**'
certificate_path = 'CURRENT_USER\\my\\***'
sms = ServiceManagementService(subscription_id, certificate_path)
result=sms.get_deployment_by_name("your service name","your deployment name")

You can get the role list and check the every role property, please see this picture:



标签: python azure