Search code examples
c#azurevirtual-machinevirtual

How to get list of Virtual Machines in Windows Azure programatically


I'm trying to write an application that lists the status of all the VM's and their statuses (running/not running) on Azure. I can get a list of all the cloud services and their names running using the following code...

var x = from h in _computeManagementClient.HostedServices.List() select h;

foreach (var item in x)
{
    //item.Properties.Label is the name of the service
}

Is there a similar method that I can use to list the VM's and their statuses? I would prefer to not use the REST API if at all possible.


Solution

  • By VMs, I assume you mean role instances?

    You can get at them through the deployment.

    var instances = _computeManagementClient.Deployments.GetBySlot(serviceName, DeploymentSlot.Production).RoleInstances;
    

    This returns a collection of Microsoft.WindowsAzure.Management.Compute.Models.RoleInstance from which you can get status information etc.

    The docs for RoleInstance:

    http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.management.compute.models.roleinstance.aspx