简体   繁体   中英

azure management libraries virtual machine state

Have been using the Azure Management Libraries for a few weeks now, but am having trouble obtaining the state (or status) of multiple virtual machines in the same cloud service. I can grab a list of the virtual machines, but cannot find a property the shows the individual vm state. So far, all I can find is the state of the parent cloud service.

var deployment=GetAzureDeyployment("mvwVM",DeploymentSlot.Production);
            if (deployment.Roles.Count>0)
            {
                foreach (var role in deployment.Roles)
                    if (role.RoleType == VirtualMachineRoleType.PersistentVMRole.ToString())
                    {
                        Console.WriteLine(role.RoleName);
                        Console.WriteLine(role.AvailabilitySetName);
                    }

How do I get an individual vm state to go along with the vm name?

Thanks.

Here is some code to use instead (I was using 2.0 of the Management Libraries):

var deployment=GetAzureDeyployment("mvwVM",DeploymentSlot.Production);

foreach (var instance in deployment.RoleInstances)
{
   Console.WriteLine("Name: {0}, State: {1}", instance.InstanceName, instance.InstanceStatus);
}

The difference here is that it goes straight to the RoleInstances and the status is on the instance. It gets a little confusing because a Cloud Service container can either have a Cloud Services (as in web or worker roles) OR one or more Virtual Machines in it. Usually Cloud Service (web/worker roles) have "Roles" and then one or more instances within a role. Since a Cloud Service deployment can hold either, the object model gets a little muddled as it needs to support both types of deployment.

The code you were using was only reaching the Role level. In a deployment of Virtual Machines each VM is a Role in the object model, which isn't necessarily the case in a web/worker role deployment. That's also why the RoleName is the name of the VM you were looking for.

The code I provided assumes I'm only looking at Virtual Machines in this deployment (since you can't mix and match web/worker roles and VMs in the same deployment) and simply goes straight to the RoleInstances property. This is a list of each individual VM in the deployment.

Hope that helps.

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