question

TBrack-4170 avatar image
0 Votes"
TBrack-4170 asked prmanhas-MSFT commented

Azure VM API - query power state for multiple VMs

We would like to be able to query the power state of all VMs in a particular resource group and/or subscription using an API. I can see power state is returned by https://docs.microsoft.com/en-us/rest/api/compute/virtualmachines/instanceview, but this only provides power state for a single, specified VM. Is there another API that can provide it for multiple VMs?

Many thanks

Tim

azure-virtual-machines
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@TBrack-4170 Any update on the issue?

If the suggested response helped you resolve your issue, do click on "Mark as Answer" and "Up-Vote" for the answer that helped you for benefit of the community.

Thanks.


0 Votes 0 ·

1 Answer

shivapatpi-MSFT avatar image
0 Votes"
shivapatpi-MSFT answered

Hello @TBrack-4170 ,
Thanks for your query ! As of now to get the power state of the machines , it needs to be queried individually if you want to go with REST API.

Similar issue was discussed here :

If you are open to other options , there are couple of ways:
-> Using Azure CLI commands
-> Using Azure PowerShell commands
-> If you are trying to wrap these REST API commands in C#/PowerShell SDK - probably you can loop through the Virtual Machines by running the REST API for each machine.
1) To get the list of all Virtual Machines in a subscription:
GET https://management.azure.com/subscriptions/{subid}/providers/Microsoft.Compute/virtualMachines?api-version=2020-12-01
2) Loop through each Virtual Machine by passing the VM name retrieved from above API : GET
https://management.azure.com/subscriptions/{subid}/resourceGroups/visualstudiovm/providers/Microsoft.Compute/virtualMachines/{VMNAME}/instanceView?
api-version=2020-12-01



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.