如果要使用 C# 列出 azure vm 资源 sku,请参考以下步骤:
- 创建服务主体并将 Contributor 角色分配给 sp
az login
#create sp and assign Contributor role at subscription level
az ad sp create-for-rbac -n "your service principal name"
- 代码。我使用包装
Microsoft.Azure.Management.Fluent
和Microsoft.Azure.Management.ResourceManager.Fluent
string clientId = "<sp appID>";
string clientSecret = "<sp passowrd>";
string tenantId = "";
string subscriptionId = "";
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(clientId,
clientSecret,
tenantId,
AzureEnvironment.AzureGlobalCloud);
var azure = Microsoft.Azure.Management.Fluent.Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders)
.Authenticate(credentials)
.WithSubscription(subscriptionId);
IPagedCollection<IComputeSku> skus = await azure.ComputeSkus.ListbyRegionAndResourceTypeAsync(Region.AsiaSouthEast, ComputeResourceType.VirtualMachines);
// process data according to your need
//get B-series VMs size
var newSkus =skus.Where(sku => sku.Inner.Family == "standardBSFamily").ToList();
更多详细信息,请参阅示例和API