Bulk Tagging Logic Apps

1 minute read

Description:

Here are the steps to tag resources in Bulk using powershell

To Resolve:

  1. Create a powershell file on your machine and paste in:

    1
    2
    3
    
    Import-Module Az
    $sub = 'someAzureSubscriptionID'
    Connect-AzAccount -SubscriptionId $sub -UseDeviceAuthentication
    
    • Where $sub if your subscription ID if you have multiple subscriptions
  2. Now add a couple line breaks and paste in the rest:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    
    $names = @(
    'MyLogicApp1-002',
    'MyLogicApp1-024',
    'MyLogicApp2-001',
    'MyLogicApp2-002',
    'MyLogicApp2-003',
    'MyLogicApp2-004',
    'MyLogicApp2-005',
    'MyLogicApp2-006',
    'MyLogicApp2-007')
    
    $count = 0
    
    foreach ($name in $names)
    {
       $count += 1
          
       $stepCount = $count.tostring()
       If ($($stepCount.Length) -gt 1)
       {
          $currentCount = "0" + $stepCount
       }
       Else
       {
          $currentCount = "00" + $stepCount
       }
          
       Write-Output "Step: $currentCount"
       $currentLA = Get-AZlogicapp -ResourceGroupName "My-Logic-App-RG" -Name $name
          
       Write-Output "Processing $($currentLA.name)"
    
       [hashtable]$currentTags = $($currentLA.Tags)
          
       Write-Verbose "Looping through key value pairs to add to $($currentLA.name)"
       $newTags = @{
          "Step"              = $currentCount
          "JobStream"         = "MyGroupName"
       }
    
       foreach ($key in $($newTags.keys) )
       {
          
          $value = $newTags[$key]
          Write-Verbose "Key to add: $key"
          Write-Verbose "Value to add: $value"
          
          If ($currenttags.ContainsKey($key) )
          {
                $currenttags.Remove($key)
                $currentTags.Add($key, $value)
          }
          Else
          {
                $currentTags.Add($key, $value)
          }
       }
    
       Set-AzResource -ResourceId $($currentLA.Id) -Tag $currentTags -Force
    
       Write-output "Added tag to $($currentLA.name) "
       #Start-Sleep -Seconds 10
    
    }
    
  3. So this will loop through a list of Logic Apps defined in $names and set various tags on them. It will only update tags or add new ones but keep all the existing ones. Feel free to use!

Comments