Created
January 14, 2022 22:43
-
-
Save vukasinterzic/cd9af8704bf0f0528c33c116c6ff6f1e to your computer and use it in GitHub Desktop.
Replace Azure Tag Name, keep the original Tag Value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Task 2: Replace Tag Name, keep the original Tag Value: | |
# Get all Az Subscriptions | |
$Subscriptions = Get-AzSubscription | |
#Define old and new tag names | |
$WrongName = "WrongTagName" | |
$CorrectName = "CorrectTagName" | |
#Subscription loop | |
foreach ($subscription in $Subscriptions) { | |
$Resources = @() | |
Write-Host "Selecting Subscription $($Subscription.Name)... " | |
Get-AzSubscription -SubscriptionName $Subscription.Name | Set-AZContext | |
$Resources = Get-AzResource -TagName $WrongName | |
#Resource loop | |
foreach ($Resource in $Resources) { | |
$ResourceTags = "" | |
$WrongTag = "" | |
$NewTag = "" | |
Write-Host "Processing resource $($Resource.Name) ..." | |
$ResourceTags = Get-AzTag -ResourceId $Resource.ResourceId | |
$Value = $ResourceTags.Properties.TagsProperty[$WrongName] | |
$WrongTag = @{$WrongName=$Value} | |
$NewTag = @{$CorrectName=$Value} | |
#Create new tag with original value | |
Update-AzTag -ResourceId $Resource.ResourceId -Tag $NewTag -Operation Merge | |
#Delete the old tag with incorrect name | |
Update-AzTag -ResourceId $Resource.ResourceId -Tag $WrongTag -Operation Delete | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment