List Azure Virtual Machines via REST API

2019-03-15 16:56发布

问题:

I am currently attempting to get a list of all of the Virtual Machines that I have running under a Windows Azure subscription programmatically. For this, I am attempting to use the Azure REST API (https://management.core.windows.net), and not use the power-shell cmdlets.

Using the cmdlets I can run 'Get-AzureVM' and get a listing of all of the VM's with ServiceName, Name, and Status without any modifications. The problem is that I cannot find anywhere in the documentation of how to list out the VMs via the API.

I have looked through the various Azure REST API's but have not been able to find anything. The documentation for VM REST API does not show or provide a list function.

Am I missing the fundamentals somewhere?

// Create the request.
            // https://management.core.windows.net/<subscription-id>/services/hostedservices
            requestUri = new Uri("https://management.core.windows.net/"
                                 + subscriptionId 
                                 + "/services/" 
                                 + operation);

This is what I am using for the base of the request. I can get a list of hosted services but not the Virtual Machines.

回答1:

You would need to get a list all the Cloud Services (Hosted Services), and then the deployment properties for each. Look for the deployment in the Production environment/slot. Then check for a role type of "PersistentVMRole".

VMs are really just a type of Cloud Service, along with Web and Worker roles. The Windows Azure management portal and PowerShell cmdlets abstracts this away to make things a little easier to understand and view.



回答2:

Follow these steps for listing VMs:

  1. List HostedServices using the following ListHostedServices
  2. For each service in from the above, a)GetDeployment by Environment(production or staging). OR b) Get Deployment By Name.
  3. In either case, get the value for Deployment.getRoleInstanceList().getRoleInstance().getInstanceName().


回答3:

You can use Azure node SDK to list out all VMs in your subscription

computeClient.virtualMachines.listAll(function (err, result))

More details on Azure Node SDK here: https://github.com/Azure-Samples/compute-node-manage-vm



标签: rest azure