Downloading Tags to manage them
  • 20 May 2020
  • 1 Minute to read
  • Contributors
  • Comment
  • Dark
    Light
  • PDF

Downloading Tags to manage them

  • Comment
  • Dark
    Light
  • PDF

Article Summary

I have a script below which you can use to point at a resource group and download the resource tags to a csv file. The script basically goes through the resources and builds up a collection of powershell objects with a property for each of the tags i am interested in. I will then export them to the csv file.

What I usually do is to download the dev/test/etc resource groups to seperate csv files so its easier to manage them. In the spreadsheet I can then modify and add tags as I wish.

Full Script


function DownloadResources([string] $csvPath, [string] $resourceGroup)
{    
    $resourceDescriptions = New-Object "System.Collections.Generic.List[System.Object]" 
     
    $resources = Get-AzResource -ResourceGroupName $resourceGroup
       
    foreach ($resource in $resources) {

        Write-Host 'Processing ' $resource.Name

        $resourceDescription = New-Object -TypeName psobject
        $resourceDescription | Add-Member -MemberType NoteProperty -Name 'Name' -Value $resource.Name        
        $resourceDescription | Add-Member -MemberType NoteProperty -Name 'Type' -Value $resource.ResourceType
        
        $governanceTags = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.Object]"         
        $governanceTags.Add('Tag_Environment', '')
        $governanceTags.Add('Tag_costCentre', '')
        $governanceTags.Add('Tag_managedBy', 'eai@acme.com')
        $governanceTags.Add('Tag_documentationLink', '')     

        #Process Tags
        if($resource.Tags -ne $null){
            foreach($tag in $resource.Tags.GetEnumerator()){  
        
                $key = $tag.Key
                $value = $tag.Value
                                  
                #Environment
                if($key.ToLower() -eq 'environment'){                   
                    $governanceTags['Tag_Environment'] = $value
                }

                #Data Profile
                if($key.ToLower() -eq 'dataprofile'){
                    $governanceTags['Tag_dataProfile'] = $value
                }

                #costCentre
                if($key.ToLower() -eq 'costcentre'){
                    $governanceTags['Tag_costCentre'] = $value
                }

                #managedBy
                if($key.ToLower() -eq 'managedby'){
                    $governanceTags['Tag_managedBy'] = $value
                }
 
                
                #documentationLink
                if($key.ToLower() -eq 'documentationLink'){
                    $governanceTags['Tag_documentationLink'] = $value
                }    
            }
        }
        

        #Add fields for Governance Tags
        foreach($governanceTag in $governanceTags.GetEnumerator()){  
        
            $csvFieldHeader = $governanceTag.Key
            $csvFieldValue = $governanceTag.Value

            $resourceDescription | Add-Member -MemberType NoteProperty -Name $csvFieldHeader -Value $csvFieldValue
        }

        #Additional Properties for useful info and ID which is used when uploading the tags
        $resourceDescription | Add-Member -MemberType NoteProperty -Name 'Id' -Value $resource.Id
        $resourceDescription | Add-Member -MemberType NoteProperty -Name 'ResourceGroup' -Value $resourcegroup


        $resourceDescriptions.Add($resourceDescription)
    }
    
    $resourceDescriptions | Export-Csv -Path $csvPath -NoTypeInformation    
}

#Dev
$path = $PSScriptRoot + '\Resources.Dev.csv'
$resourceGroup = 'MyRG-Dev'
DownloadResources -csvPath $path -resourceGroup $resourceGroup


Was this article helpful?