Forum Discussion

nixthecloud's avatar
nixthecloud
Copper Contributor
Sep 02, 2022

Azure Tags: Assistance w/ PS Scripting or ARM Template & Pipeline

I've been tasked to develop a script, or automate via other means (ARM template and pipeline for example).  The automation must do the following:

 

  1. Prompt for the Azure tenant ID and subscription ID (or Name).

  2. Prompt for Azure Credentials.

  3. Set Azure Context.

  4. Set the Tag Name and Tag Value.

  5. Get a list of all Resource Groups in that Subscription and export that list to a text file or csv.

  6. Call the Resource Group text file and for each Resource Group listed in the text file or csv, get the resource inventory in that RG, and export that RG's inventory into a separate text file which will list each resource by Name, Type, Location, Tags.

  7. Call each RG inventory text file and for each inventory item (resource) in the text file, check the Tags property and either create the Tag Name and Value (noted in #3 above), OR if it does not match the Name and value in #3, update the Tag.

I'm certain there is very likely a PowerShell script (preference) or a template and pipeline out there that can achieve the requirement, but I've yet to find one.

 

I have started working on a basic PowerShell script (below) to get the nuts and bolts in there while testing the latest AZ commands, and it works, but now I must build the logic in the script to automate #'s 5 - 7 above.

1 Reply

  • Takae this:

     

    1. Prompt for Tenant ID, Subscription ID/Name, and Credentials

    $tenantId = Read-Host "Enter Azure Tenant ID"
    $subscriptionId = Read-Host "Enter Azure Subscription ID or Name"
    Connect-AzAccount -TenantId $tenantId
    Set-AzContext -SubscriptionId $subscriptionId

    2. Prompt for Tag Name and Value

    $tagName = Read-Host "Enter Tag Name"
    $tagValue = Read-Host "Enter Tag Value"
    $tagPair = @{ $tagName = $tagValue }

    3. Get Resource Groups and Export to CSV

    $rgList = Get-AzResourceGroup
    $rgList | Select-Object ResourceGroupName, Location | Export-Csv -Path "./ResourceGroups.csv" -NoTypeInformation

    4. Loop Through RGs and Export Inventory

    $rgCsv = Import-Csv "./ResourceGroups.csv"
    foreach ($rg in $rgCsv) {
        $resources = Get-AzResource -ResourceGroupName $rg.ResourceGroupName
        $resources | Select-Object Name, ResourceType, Location, Tags |
            Export-Csv -Path "./Inventory_$($rg.ResourceGroupName).csv" -NoTypeInformation
    }

    5. Loop Through Inventory Files and Apply Tags

    $inventoryFiles = Get-ChildItem -Path "." -Filter "Inventory_*.csv"
    foreach ($file in $inventoryFiles) {
        $inventory = Import-Csv $file.FullName
        foreach ($item in $inventory) {
            $resource = Get-AzResource -Name $item.Name -ResourceGroupName ($file.BaseName -replace "Inventory_", "")
            $existingTags = $resource.Tags
    
            if (-not $existingTags.ContainsKey($tagName) -or $existingTags[$tagName] -ne $tagValue) {
                $updatedTags = $existingTags
                $updatedTags[$tagName] = $tagValue
                Update-AzTag -ResourceId $resource.ResourceId -Tag $updatedTags -Operation Merge
            }
        }
    }

     

Resources