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.
3 Answers
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

- 11,887
- 6
- 38
- 74
-
great... what comes for api-version btw? – Muhammad Murad Haider Jan 21 '16 at 19:29
-
@MuhammadMuradHaider There is a link at the top of the page that gives [required headers and parameters](https://msdn.microsoft.com/en-us/library/azure/mt163630.aspx) including the api-version - just to spoil the surprise it is `2015-05-01-preview` ;) – Michael B Jan 21 '16 at 19:35
-
1First link is broken for me but looks like it's now documented here: https://learn.microsoft.com/en-us/rest/api/compute/virtualmachinesizes/list – Pat Myron Apr 29 '19 at 17:49
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

- 780
- 7
- 16
-
can you please put some light on certificate based authentication? – Muhammad Murad Haider Jul 21 '16 at 15:46
-
-
Thanks, It seems to be requiring to upload a certificate for the AD application first. Is it done through powershell or there are other ways too? – Muhammad Murad Haider Jul 22 '16 at 12:18
-
1please follow below link https://azure.microsoft.com/en-in/documentation/articles/api-management-howto-mutual-certificates/ https://azure.microsoft.com/en-us/blog/using-certificates-in-azure-websites-applications/ – Mehul Bhalala Jul 25 '16 at 04:44
-
Any kind of `SubscriptionCloudCredentials` object will work. -- i.e. you can do it with a username / password as well, using a `TokenCloudCredentials`. – BrainSlugs83 May 11 '17 at 21:57
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();

- 780
- 7
- 16