Programmatically getting the list of azure virtual machine sizes

允我心安 提交于 2021-02-19 03:07:55

问题


I am new to Azure management libraries for .net. How can we enumerate VM instance sizes available with respect to subscription or in general using Azure Management libraries for .Net or Rest APIs? Please suggest.


回答1:


You can get a list of VM sizes for a region by calling

https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.Compute/locations/{location}/vmSizes?api-version={api-version}

As documented here - List all available virtual machine sizes in a region

There is also a .Net Class for the same, but I've not found any examples of it being used - documented here - VirtualMachineSizeOperationsExtensions.List




回答2:


You can get list of VM sizes by region fillter

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();

you must need create one native client api on Azure Active Directory for token base authentication otherwise you can also use certification base authentication for client authorization.

i am using Microsoft.Azure.Management.Compute.dll, v10.0.0.0 for compute resources.

you can download here: https://www.nuget.org/packages/Microsoft.Azure.Management.Compute/13.0.4-prerelease




回答3:


You can get list of VM Size by using Certificate Base Authentication

Get Certificate method

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));
    }

here i describe same way to get VM size

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();


来源:https://stackoverflow.com/questions/34930835/programmatically-getting-the-list-of-azure-virtual-machine-sizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!