![](/img/trans.png)
[英]How to list resources in a resource group with Microsoft.Azure.Management.Fluent?
[英]How to get List of resources by resource type and resource group by using Azure Resource management apis
如何使用 Azure 資源管理 API 獲取資源組的資源列表
我已經安裝了 Microsoft.Azure.Management.ResourceManager.Fluent Nuget 包 下面的腳本只給我資源組列表,而不是每個資源組的資源列表。
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud);
var azure = Azure.Configure().Authenticate(credentials).WithSubscription(subscriptionID);
var resourecelist = azure.ResourceGroups.List().ToList();
我正在尋找類似於 powershell 中可用的東西
Get-AzureRmResource -ResourceGroupName $batchResourceGroup -ResourceType 'Microsoft.Batch/batchAccounts'
請嘗試以下代碼以獲取資源列表。 我在我這邊測試它,它工作正常。 我們也可以使用Resources - List By Resource Group Rest API 來做到這一點。
var resouceManagementClient = new ResourceManagementClient(credentials) {SubscriptionId = subscriptionId};
var resource = resouceManagementClient.ResourceGroups.ListResourcesAsync(resourceGroup,new ODataQuery<GenericResourceFilterInner>(x=>x.ResourceType == "Microsoft.Batch/batchAccounts")).Result;
上面的答案已經過時,所以這是我在 2020 年 12 月使用的代碼片段。
Azure.IAuthenticated _azure;
string _subscriptionId;
RestClient _restClient;
async Task Main()
{
Connect();
// Get resource groups
var resourceManagementClient = new ResourceManagementClient(_restClient)
{
SubscriptionId = _subscriptionId
};
var resourceList = (await resourceManagementClient.ResourceGroups.ListAsync()).ToList().OrderBy(r => r.Name);
// ...
}
void Connect()
{
_subscriptionId = "XXX";
var tenantId = "YYY";
var clientId = "ZZZ";
var secret = "QQQ";
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
clientId, secret, tenantId,
AzureEnvironment.AzureGlobalCloud)
.WithDefaultSubscription(_subscriptionId);
_restClient = RestClient
.Configure()
.WithEnvironment(AzureEnvironment.AzureGlobalCloud)
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.WithCredentials(credentials)
.Build();
var creds = new AzureCredentialsFactory().FromServicePrincipal(
clientId, secret, tenantId,
AzureEnvironment.AzureGlobalCloud
);
_azure = Azure.Authenticate(creds);
}
使用/導入/NuGet。 (你不需要所有這些......):
Microsoft.Azure.Management.AppService.Fluent
Microsoft.Azure.Management.AppService.Fluent.Models
Microsoft.Azure.Management.Fluent
Microsoft.Azure.Management.ResourceManager.Fluent
Microsoft.Azure.Management.ResourceManager.Fluent.Authentication
Microsoft.Azure.Management.ResourceManager.Fluent.Core
Microsoft.IdentityModel.Clients.ActiveDirectory
Microsoft.Rest
Microsoft.ServiceBus.Messaging
System.Threading.Tasks
Microsoft.Rest.Azure
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.