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

NixCloud 1 Reputation point
2022-09-01T12:29:04.26+00:00

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 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.
  6. Call the Resource Group text file and for each Resource Group listed in the text file, 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 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.

#Prompts User for AZ Tenant and Subscription ID's  
$TenantID = Read-Host "Please Enter the Tenant ID"  
$SubId = Read-Host "Please Enter the Subscription ID"  
  
#Authenticates User to Azure  
Connect-AzAccount -TenantId $TenantID  
Set-AzContext -Subscription $SubId  
  
#Prompts User for RG Name and Creates Log File  
$RGName = Read-Host "Please Enter the Name of the Resource Group"  
  
$LogFile = "c:\temp\pslogfiles\"+$RGName+".txt"  
  
#Gets All Resources in the RG and Appends Data in the Log File  
Get-AzResource -ResourceGroupName $RGName | select Name,Type,Location,Tags,Id | Out-File $LogFile  
  
#Creates Tags in the Resource Group and Assigns Tags to the Designated Resource  
$ResourceId = Read-Host "Open the Log File Created in c:\temp\pslogfiles and Copy & Paste the Full Id of the Resource You Wish to Edit"  
$TagName = Read-Host "Please Enter the Tag Name"  
$TagValue = Read-Host "Please Enter the Tag Value"  
$Tag = @{"$TagName"="$TagValue"}  
Set-AzResourceGroup -Name $RGName -Tag $Tag  
New-AzTag -ResourceId $ResourceId -Tag $Tag  

Thank you!

Azure Migrate
Azure Migrate
A central hub of Azure cloud migration services and tools to discover, assess, and migrate workloads to the cloud.
717 questions
Azure Resource Mover
Azure Resource Mover
An Azure service used for moving multiple resources between Azure regions.
200 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Luke Murray 10,526 Reputation points MVP
    2023-05-14T20:39:12.4633333+00:00

    Try this:

    # Prompt for Azure subscription ID (or Name)
    $subscriptionId = Read-Host "Please enter the Azure subscription ID or Name"
    
    # Prompt for Azure credentials
    $azureCredentials = Get-Credential -Message "Please enter your Azure credentials"
    
    # Set Azure context
    Connect-AzAccount -Credential $azureCredentials -Subscription $subscriptionId | Out-Null
    
    # Set the Tag Name and Tag Value
    $tagName = Read-Host "Please enter the Tag Name"
    $tagValue = Read-Host "Please enter the Tag Value"
    
    # Get a list of all Resource Groups in that Subscription and export that list to a text file
    $rgList = Get-AzResourceGroup | Select-Object ResourceGroupName | Out-File -FilePath "ResourceGroups.txt"
    
    # For each Resource Group listed in the text file, 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
    foreach ($rg in Get-Content "ResourceGroups.txt") {
        $rgInventory = Get-AzResource -ResourceGroupName $rg | Select-Object Name, Type, Location, Tags | Out-File -FilePath "$rg-Inventory.txt"
    
        # 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
        foreach ($resource in Get-Content "$rg-Inventory.txt") {
            $resourceTags = ($resource | ConvertFrom-Json).Tags
            if ($resourceTags.ContainsKey($tagName) -and $resourceTags[$tagName] -eq $tagValue) {
                Write-Host "Resource $resource already has the correct tag"
            }
            else {
                $resourceId = ($resource | ConvertFrom-Json).ResourceId
                $newTag = @{ $tagName = $tagValue }
                Set-AzResource -ResourceId $resourceId -Tag $newTag
                Write-Host "Tag added to resource $resource"
            }
        }
    }
    
    0 comments No comments