Home Linux & Systems Cybersecurity Cloud & DevOps Networks & Infrastructure SIEM & Monitoring DFIR & Threat Intel Development & Other All categories Projects About Tools

Bulk update tags in Azure using PowerShell

Leer en espanol
Bulk update tags in Azure using PowerShell

Table of contents

In this article, we'll explore a PowerShell script that allows you to update tags on a resource group and all resources within that group in Azure. Next, from ===

Introduction

In this article, we will explore a PowerShell script that allows update tags on a resource group and on all resources within that group in Azure. Below we'll detail how each part of the script works, what each command does, and how you can adapt it to your needs.Script Purpose

This script aims to update or add a specific tag (in this case "ID") with a value defined in a resource group and in all resources within that group. This is useful when you need to maintain a uniform and consistent labeling structure across multiple resources in an Azure group.

Prerequisites

Before running this script, make sure that:

  1. Do you have the module installed? Az PowerShell, which is required to interact with Azure.
  2. You are authenticated to your Azure account using Connect-AzAccount.

If you don't have the module Az installed, you can do it with the following command:

TERRAFORM
Install-Module -Name Az -AllowClobber -Scope CurrentUser

Breaking down the script

1. Definition of variables

The script starts by defining three main variables:

PHP
$resourceGroupName = "DEV-REDORBITA-01"  # Nombre del grupo de recursos
$newTagValue = "XXXXXXXXX"               # Nuevo valor para la etiqueta ID
$tagName = "ID"                          # Nombre de la etiqueta
  • $resourceGroupName: Here you define the name of the resource group where you want to update the tags. In this case, the resource group is called "DEV-REDORBITA-01", but you can modify it according to your environment.
  • $newTagValue: This is the value that will be assigned to the tag. In the example, it is defined as "XXXXXXXXX", but you should replace it with the value you want to assign to the tag in your resources.
  • $tagName: Is the name of the tag to be updated or added. In this case, the label is called "ID", but you can adjust it to your needs.

2. Update tag in resource group

Once the variables are defined, the script proceeds to update the label in the resource group.

PHP
Write-Output "Actualizando etiqueta en el grupo de recursos $resourceGroupName..."
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
$resourceGroup.Tags[$tagName] = $newTagValue
Set-AzResourceGroup -Name $resourceGroupName -Tag $resourceGroup.Tags
  • Get-AzResourceGroup -Name $resourceGroupName: This command obtains the information of the resource group that we have specified.
  • $resourceGroup.Tags[$tagName] = $newTagValue: Here, the value of the tag defined by is assigned $tagName with the new value $newTagValue to the resource group.
  • Set-AzResourceGroup -Name $resourceGroupName -Tag $resourceGroup.Tags: This command applies the tag changes to the resource group.

The result will be that the label "ID" in the resource group will be updated with the value "XXXXXXXXX".

3. Get all resources within the resource group

The next step is to get all the resources within the specified resource group:

PHP
Write-Output "Obteniendo todos los recursos dentro del grupo $resourceGroupName..."
$resources = Get-AzResource -ResourceGroupName $resourceGroupName
  • Get-AzResource -ResourceGroupName $resourceGroupName: This command gets all the resources that belong to the resource group defined in $resourceGroupName.

4. Update tags on each resource within the resource group

Once all the resources are obtained, the script loops through each one of them and updates the tags:

PHP
foreach ($resource in $resources) {
    Write-Output "Actualizando etiqueta en el recurso $($resource.Name)..."
    $resourceTags = $resource.Tags
    if (-not $resourceTags) {
        $resourceTags = @{} # Si el recurso no tiene etiquetas, crear un diccionario vacío
    }
    $resourceTags[$tagName] = $newTagValue
    Set-AzResource -ResourceId $resource.ResourceId -Tag $resourceTags -Force
}
  • foreach ($resource in $resources): The script loops through all previously obtained resources.
  • $resource.Tags: Gets the current tags for the resource.
  • if (-not $resourceTags): If the resource has no tags, an empty dictionary is initialized.
  • $resourceTags[$tagName] = $newTagValue: Assigns the new value of the label "ID" to the resource.
  • Set-AzResource -ResourceId $resource.ResourceId -Tag $resourceTags -Force: This command applies the new tags to the resource.

5. Ending

Once all tags have been updated in the resource group and all resources within it, a confirmation message is displayed:

TERRAFORM
Write-Output "Actualización de etiquetas completada."

This is the final message indicating that the script has completed its execution and all tags have been updated successfully.

Complete code

PHP

#Connect-AzAccount
 
# Variables
$resourceGroupName = "DEV-REDORBITA-01" 
$newTagValue = "XXXXXX"                    
$tagName = "ID"                      
 
Write-Output "Actualizando etiqueta en el grupo de recursos $resourceGroupName..."
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
$resourceGroup.Tags[$tagName] = $newTagValue
Set-AzResourceGroup -Name $resourceGroupName -Tag $resourceGroup.Tags
 
Write-Output "Obteniendo todos los recursos dentro del grupo $resourceGroupName..."
$resources = Get-AzResource -ResourceGroupName $resourceGroupName
 
foreach ($resource in $resources) {
    Write-Output "Actualizando etiqueta en el recurso $($resource.Name)..."
    $resourceTags = $resource.Tags
    if (-not $resourceTags) {
        $resourceTags = @{} 
    }
    $resourceTags[$tagName] = $newTagValue
    Set-AzResource -ResourceId $resource.ResourceId -Tag $resourceTags -Force
}
 
Write-Output "Actualización de etiquetas completada."

:wq!

Comments