简体   繁体   中英

Get Azure VM using resource manager deployment and rest api

I've deployed a vm using Resource Manager deployment model.

Using rest api as described here I'm able to get infomation about the VM.
I'm looking to get the power state, ip address, and machine size. However to get all that information I need 3 different calls https://management.azure.com/subscriptions/ {SubscriptionId}/resourceGroups/{ResourceGroup}/providers/Microsoft.Compute/virtualmachines/{ServerName}

https://management.azure.com/subscriptions/ {SubscriptionId}/resourceGroups/{ResourceGroup}/providers/Microsoft.Compute/virtualmachines/{ServerName}/InstanceView

https://management.azure.com/subscriptions/ {SubscriptionId}/resourceGroups/{ResourceGroup}/providers/Microsoft.Network/networkInterfaces/{ServerName}_NIC

Is there a way to get all this information in 1 call?

Since VM deployed with Resource Manager then state, ip address and size info under the different providers (Compute and Network). It maybe has no way to get the VM info and network info in a call currently.

With Microsoft Azure Management Client Library (Fluent) , we can get the VM info (power state, machine size ,ip address). Actually, it call the REST API twice . About Azure authentication please refer to how to create an authentication file .

AzureCredentials credentials = AzureCredentials.FromFile("Full path of your AzureAuthFile");
                var azure = Azure
                    .Configure()
                    .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC)
                    .Authenticate(credentials)
                    .WithDefaultSubscription();
  foreach (var virtualMachine in azure.VirtualMachines.ListByGroup("Your Resource Group Name").Where(virtualMachine => virtualMachine.ComputerName.Equals("vmName")))
                    {
                        var state = virtualMachine.PowerState;
                        var size = virtualMachine.Size;
                        var ip = virtualMachine.GetPrimaryPublicIpAddress().IpAddress; //call Rest API again
                    }

If it deployed under the CloudService then we can use Windows Azure management library . It is easy to get VM(Role) Info about power state, ip address, and machine size.

var certificate = new CertificateCloudCredentials(subscriptionId, x509Certificate);
var computeManagementClient = new ComputeManagementClient(certificate);
var deployments = await computeManagementClient.Deployments.GetByNameAsync (hostedServiceName,"Your Deployment Name");
var state = deployments.RoleInstances.First().PowerState;
var ipAddress = deployments.RoleInstances.First().IPAddress;
var size = deployments.RoleInstances.First().InstanceSize;

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