Last active
January 14, 2022 22:47
-
-
Save vukasinterzic/1ba6d75c43a2b8d9d271c50b4e44ef92 to your computer and use it in GitHub Desktop.
Replace Azure Tag Value, keep the Tag Name
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 1: Replace Tag Value, keep Tag Name: | |
# Get all Az Subscriptions | |
$Subscriptions = Get-AzSubscription | |
$TagName = 'YourTagName' | |
$WrongValue = 'wrong tag value' | |
$CorrectValue = 'new tag value' | |
$WrongTag = @{$TagName=$WrongValue} | |
$NewTag = @{$TagName=$CorrectValue} | |
#subscription loop | |
foreach ($subscription in $Subscriptions) { | |
$Resources = @() | |
Write-Host "Selecting Subscription $($Subscription.Name)... " | |
Get-AzSubscription -SubscriptionName $Subscription.Name | Set-AZContext | |
$Resources = Get-AzResource -Tag $WrongTag | |
foreach ($Resource in $Resources) { | |
Write-Host "Processing resource $($Resource.Name) ..." | |
Update-AzTag -ResourceId $Resource.ResourceId -Tag $NewTag -Operation Merge | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment