skip to Main Content

I currently have a Powershell script that exports tags from azure to a CSV file, what i would like to do is update some values on this file and then through Powershell use this file to update the tags on azure.

Below is a test example of the excel file, Name being the resource groups
File example

in this scenario the tags are for the resource groups, the tags have different values and i haven’t found a good solution to do large update on these values for example a tag might have X,Y,Z,K as a value but i need to add T and replace some of the Y’s with W.

What i found for bulk updating tags is updating all the tags with the same value or individually update the tags per resource group which is not a solution as there would be hundreds of resource groups.

Any help on this would be greatly appreciated.

Tried multiple scripts as well as the cmdlets from Microsoft docs however these do not work for what i need.

2

Answers


  1. One approach you might take is to use a template engine. If you can somehow come up with a template that describes the transformation to be done on a single record of your .csv file, then the remaining thing to be done is to apply the csv file to the template.

    This is extremely simple, but it doesn’t address your need to do bulk updates. What is important about bulk updates to tags? Is it efficient use of machine resources? Is it reliability of the process?

    If you do decide to go down the template engine route, I have a very small template engine that is driven off of CSV data. It follows here:

    <#
    .NOTES
        Script: Expand-Csv    Rev:  3.2
        Author: DGC           Date: 2-21-19
    .SYNOPSIS
        Generates multiple expansions of a template,
        driven by data in a CSV file.
    .DESCRIPTION
        This function is a table driven template tool. 
    
        It generates output from a template and
        a driver table.  The template file contains plain
        text and embedded variables.  The driver table 
        (in a csv file) has one column for each variable, 
        and one row for each expansion to be generated.
    #>
    function Expand-csv {
        [CmdletBinding()]
        Param (
           [Parameter(Mandatory=$true)] [string] $driver,
           [Parameter(Mandatory=$true)] [string] $template
        )
        Process {
           Import-Csv $driver | % {
               $_.psobject.properties | % {Set-variable -name $_.name -value $_.value}
               Get-Content $template | % {$ExecutionContext.InvokeCommand.ExpandString($_)} 
           }
        }
    }
    

    This is a bare bones, no frills implementation of a template engine. THere are a number of other template engines available, some of them more sophisiticated. If you want to see a couple of demos of this one to show the functionality, you can visit my repo at github: github.com/dcressey/expand-csv

    Login or Signup to reply.
    1. To replace the existed tag value with a new value, you can run below script with the

    Update-AzTag

    $resourcegroup = Get-Azresourcegroup 
    $tag = $resourcegroup.tags
    foreach($rg in $resourcegroup){                                                        
     if($tag.Created -eq "date"){
     $replacedTags = @{"Created"="info"}
     Update-AzTag -ResourceId $rg.ResourceId -Tag $replacedTags -Operation Replace
       }
    }
    

    enter image description here

    enter image description here

    1. To merge/add a new value with an existing one, run the PowerShell script below and change the operation flag to "Merge". If you want to merge/add the tags conditionally, add an if loop under foreach as shown above.
    foreach($rg in $resourcegroup){                                                        
    $mergeTags = @{"Deploy"="Prod"}
    Update-AzTag -ResourceId $rg.ResourceId -Tag $mergeTags -Operation Merge
    }
    

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search