Search code examples
azuretagsazure-powershellazure-cliazureportal

Recursively list all resource tags within Azure Resource Groups


We have a large number of Azure Subscriptions which currently run into the hundreds.

I'm looking to generate a report (ideally using Azure Powershell or Azure CLI) to recursively extract a list of all tags assigned to every single resource within every resource group, for between 40-50 of the subscriptions.

Currently, I can list all tags assigned at Resource Group level, but I simply can't find a way to list the tags assigned to the individual resources within each Resource Group. The list of subscriptions and resource groups on which I'd like to extract this report, are saved in a CSV file which includes two columns displaying the Subscription name and Resource Group respectively.

Any tips on how to achieve the above would be fantastic and most appreciated.


Solution

  • Not detailed code but the idea here.

    1.You should write a loop, in the loop, change the subscription each time by using this cmdlet:

    Set-AzContext -Subscription $subscription_name.

    2.Then get all the resource group in the specified subscription by using this cmdlet:

    $resource_groups = Get-AzResourceGroup
    

    3.Then write a nested loop(loop for each resource group), in this nested loop, use this cmdlet to get all azure resources within a resource group:

    foreach($rg in $resource_groups){
       $azure_resources = Get-AzResource -ResourceGroupName $rg.ResourceGroupName
    }
    

    4.Write another nested loop in step 3, this loop is used to go though all the azure resources within the specified resource group. Then use the code below to fetch tags for each azure resource within the resource group:

    foreach($r in $azure_resources){
         #the following code can get all the tags for one resource
         $r.tags
    
    }