代码之家  ›  专栏  ›  技术社区  ›  kumar

如何使用Azure资源管理API按资源类型和资源组获取资源列表

  •  2
  • kumar  · 技术社区  · 9 年前

    我已经安装了Microsoft。蔚蓝色的经营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();
    

    Get-AzureRmResource -ResourceGroupName $batchResourceGroup -ResourceType 'Microsoft.Batch/batchAccounts'
    
    2 回复  |  直到 9 年前
        1
  •  3
  •   Tom Sun    9 年前

    请尝试以下代码以获取资源列表。我测试了一下,它工作正常。我们也可以使用 Resources - List By Resource Group RESTAPI来实现这一点。

      var resouceManagementClient = new ResourceManagementClient(credentials) {SubscriptionId = subscriptionId};
      var resource = resouceManagementClient.ResourceGroups.ListResourcesAsync(resourceGroup,new ODataQuery<GenericResourceFilterInner>(x=>x.ResourceType == "Microsoft.Batch/batchAccounts")).Result;
    

    enter image description here

        2
  •  1
  •   Dennis    5 年前

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

    usings/imports/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