2

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. Commented Jan 3, 2017 at 21:35
  • 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. Commented Dec 14, 2020 at 5:26

2 Answers 2

1

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...

Sign up to request clarification or add additional context in comments.

4 Comments

I know the documentation says to use the statusOnly=true parameter to get the status of all the VMs, however that flag has no affect.
@Rossini Did you try it with which Endpoint? Because I believe the 2nd Endpoint on your question is deprecated. I could not found any documentation related to it. Also I believe it won't work with this flag/parameter. I just made a request with postman, using the specific endpoint I specified here (which differs on path and version of yours), setting that parameter to true, and I got back all the information, including the instanceView object, with all the PorwerStates of my VMs.
I missed that you are using a different end point. I'm using the Virtual Machines - List end point learn.microsoft.com/en-us/rest/api/compute/virtualmachines/list where as you are using the List all end point learn.microsoft.com/en-us/rest/api/compute/virtualmachines/… the List All end point is exactly what I am looking for, thanks for the help
Infuriatingly, this work. You'd think they would allow getting this info as additional information on top of vm data but no, you need to make a separate query.
0

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

1 Comment

how do i pass the client id and secret/ or how to get token?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.