Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Azure资源管理API按资源类型和资源组获取资源列表_C#_Azure_Azure Resource Manager - Fatal编程技术网

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

C# 如何使用Azure资源管理API按资源类型和资源组获取资源列表,c#,azure,azure-resource-manager,C#,Azure,Azure Resource Manager,如何使用Azure资源管理API获取资源组的资源列表 我已安装Microsoft.Azure.Management.ResourceManager.Fluent Nuget软件包 下面的脚本只提供资源组列表,而不是每个资源组的资源列表 var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment

如何使用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'

请尝试以下代码以获取资源列表。我在我这边测试,它工作正常。我们也可以使用RESTAPI来实现这一点

  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

安装nuget后,我也无法访问Microsoft.Azure.Management.ResourceManager.Fluent为什么?无法使用类ResourceManagementClient该怎么办?遗憾的是,使用最新的库ResourceManagementClient似乎不接受构造函数上的AzureCredentials,而是接受RestClient。因此,这个答案在安装nuget之后似乎有点过时。我也无法引用Microsoft.Azure.Management.ResourceManager.Fluent为什么?无法使用类ResourceManagementClient该怎么办?