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:
- Do you have the module installed?
AzPowerShell, which is required to interact with Azure. - 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:
Install-Module -Name Az -AllowClobber -Scope CurrentUser
Breaking down the script
1. Definition of variables
The script starts by defining three main variables:
$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.
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
$tagNamewith the new value$newTagValueto 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:
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:
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:
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
#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