以编程方式获取azure虚拟机大小列表

时间:2016-01-21 17:50:32

标签: azure azure-management-api

我是.net的Azure管理库的新手。我们如何枚举可用于订阅的VM实例大小,或者通常使用.Net或Rest API的Azure管理库? 请建议。

3 个答案:

答案 0 :(得分:5)

您可以通过调用

获取区域的VM大小列表
https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.Compute/locations/{location}/vmSizes?api-version={api-version}

如此处所述 - List all available virtual machine sizes in a region

同样有一个.Net类,但我没有找到任何使用它的例子 - 在此处记录 - VirtualMachineSizeOperationsExtensions.List

答案 1 :(得分:0)

您可以按区域填充程序获取VM大小列表

AuthenticationContext authenticationContext = new AuthenticationContext("https://login.windows.net/tenantdomainname.onmicrosoft.com"]);
UserCredential uc = new UserCredential(authusername,authpassword);
token = authenticationContext.AcquireToken("https://management.core.windows.net/", nativetenantid, uc);

var credentials = new TokenCredentials(token);
var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = subscriptionid};
var virtualMachineSize = computeClient.VirtualMachineSizes.List(region_name).ToList();

您必须在Azure Active Directory上创建一个本机客户端API才能进行令牌基本身份验证,否则您也可以使用认证基本身份验证进行客户端授权。

我正在使用 Microsoft.Azure.Management.Compute.dll,v10.0.0.0 来获取计算资源。

您可以在此处下载:https://www.nuget.org/packages/Microsoft.Azure.Management.Compute/13.0.4-prerelease

答案 2 :(得分:0)

您可以使用证书基本身份验证

获取VM大小列表

获取证书方法

private static X509Certificate2 GetStoreCertificate(string subscriptionId, string thumbprint)
    {
        List<StoreLocation> locations = new List<StoreLocation>
        { 
            StoreLocation.CurrentUser, 
            StoreLocation.LocalMachine
        };

        foreach (var location in locations)
        {
            X509Store store = new X509Store(StoreName.My, location);
            try
            {
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                X509Certificate2Collection certificates = store.Certificates.Find(
                X509FindType.FindByThumbprint, thumbprint, false);
                if (certificates.Count == 1)
                {
                    return certificates[0];
                }
            }
            finally
            {
                store.Close();
            }
        }

        throw new ArgumentException(string.Format("A Certificate with Thumbprint '{0}' could not be located.",thumbprint));
    }

这里我描述了获取VM大小的相同方法

private static X509Certificate2 Certificate = GetStoreCertificate(Your-subscriptionid,Your-thumbprint);
Microsoft.Azure.CertificateCloudCredentials credentials = new Microsoft.Azure.CertificateCloudCredentials(Your-subscriptionid, Certificate);

 var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = Your-subscriptionid};
 var virtualMachineSize = computeClient.VirtualMachineSizes.List(Your-region_name).ToList();