简体   繁体   中英

List Azure Virtual Machines via REST API

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM