【问题标题】:Get Azure VM using resource manager deployment and rest api使用资源管理器部署和 rest api 获取 Azure VM
【发布时间】:2017-04-05 01:25:12
【问题描述】:

我已经使用资源管理器部署模型部署了一个虚拟机。

Using rest api as described here 我可以获取有关 VM 的信息。
我正在寻找电源状态、IP 地址和机器大小。但是,要获得所有这些信息,我需要 3 个不同的电话 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

有没有办法在一次通话中获取所有这些信息?

【问题讨论】:

    标签: c# rest azure


    【解决方案1】:

    由于 VM 部署了资源管理器,因此在不同的提供程序(计算和网络)下状态、IP 地址和大小信息。目前可能无法在通话中获取虚拟机信息和网络信息。

    使用Microsoft Azure Management Client Library (Fluent),我们可以获得VM信息(电源状态,机器大小,IP地址)。实际上,它调用了 REST API 两次。关于 Azure 认证请参考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
                        }
    

    如果它部署在 CloudService 下,那么我们可以使用Windows Azure management library。很容易得到VM(Role) 有关电源状态、IP 地址和机器大小的信息。

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多