2
votes

I'm trying to get the status of all the VMs in a resource group using rest interface for Azure RM.

I can get the status of 1 VM by using the URL https://management.azure.com/subscriptions/[SubscriptionId]/resourceGroups/[ResourceGroup]/providers/Microsoft.Compute/virtualmachines/[serverName]?$expand=instanceView

and I can get all the VMs using this URL https://management.azure.com/subscriptions/[SubscriptionId]/resourceGroups/[ResourceGroup]/providers/Microsoft.Compute/virtualmachines

however this doesn't return the status of the VMs. I tried passing $expand=instanceView but that doesn't have any affect when I'm getting all the VMs.

2
I doubt you can do that since the instanceView thing is tied to the VM resource, not the resource group or Microsoft.Compute provider. You'll have to iterate though the VMs and call $expand=instanceView.evilSnobu
I know it has been 3 years since this question was made. But my answer is the correct way to do it today, when this is an actual and recurring need.Vitox

2 Answers

0
votes

To get all the status from VMs, in just one API request/call you can use:

GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Compute/virtualMachines?api-version=2020-06-01&statusOnly=true

From this Azure RESTful API - Virtual Machines - List All documentation:

statusOnly=true enables fetching run time status of all Virtual Machines in the subscription.

So setting statusOnly=true allows you to receive back the provisioningState property and an instanceView object, which contains the PowerState like "PowerState/running", "PowerState/stopped", etc...

Also, there is a lot of other useful and important information inside the returned instanceView object. More about it on this documentation.


After hours searching, that's the only way I found to get back all instances' status in just one call. All other approaches that I saw were using a loop, and executing one API request for every VM, which is awful slow, and depending on the application and number of VMs, unacceptable...

0
votes

this doesn't return the status of the VMs. I tried passing $expand=instanceView but that doesn't have any affect when I'm getting all the VMs.

From this documentation, we could find that GET /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}?api-version=2016-03-30[&$expand] enable us to use the $expand option to expand property, but GET /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines?api-version=2016-03-30 does not support the $expand option. Perhaps the API method that lists all of the virtual machine in the specified resource group does not return an IQueryable or does not allow query options.

As evilSnobu said, you could try to do it in two steps: list all VMs in the specified resource group and iterate though all VMs and extract name property, and then send request to retrieve information of specified VM and use $expand in the query string of the request to expand instanceView property.

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualMachines/{2}?api-version=2016-03-30&$expand=instanceView", subscriptionid, resourcegroup, vmname));

request.Method = "GET";
request.Headers["Authorization"] = "Bearer " + token;

enter image description here