I am pretty new to powershell and please bear with me. I am building a script wherein I need to replace one of the key value pairs of a Tag of any resources in a certain subscription. It can be the "Name" or "Value" of a Tag.
I got a script that will find and old Tag key and replace it with a new Tag key. It checks all resources and locate the old tag and replace it with a new tag successfully. Now, my dilemma is it only do the job for the "Name" part of Tag but not with the "Value" of the tag. Can anyone help me on how to do the "Value" part of the Tag, please.
Over all, the code will search for a tag from $oldKey across all the resources and replace it with $newKey. It does create a report of what resources it made changes and put it to a csv file.
#Define old Tag key $oldkey and new Tag key $newKey
$oldKey = "camp"
$newKey = "Camp3"
#Find ResourceGroups with oldKey, backup findings to CSV, migrate existing oldKey value to newKey merging with existing tags, then delete oldKey.
$rgsOldKeyBackup = Get-AzResourceGroup | Where-Object {$_.Tags.Keys -match $oldKey}
$rgsOldKeyBackup.count
if ($rgsOldKeyBackup) {
Get-AzResourceGroup | Where-Object {$_.Tags.Keys -match $oldKey} | Out-File "C:tempAzRGs-Tag-Backup-$oldkey.csv"
$rgs = Get-AzResourceGroup | Where-Object {$_.Tags.Keys -match $oldKey}
$rgs | ForEach-Object {
$rgOldKeyValue = $_.Tags.$oldKey
$rgNewTag = @{$newKey=$rgOldKeyValue}
$rgOldTag = @{$oldKey=$rgOldKeyValue}
$resourceID = $_.ResourceId
Update-AzTag -ResourceId $resourceID -Tag $rgNewTag -Operation Merge
$Check = Get-AzResourceGroup -Id $resourceID | Where-Object {$_.Tags.Keys -match $newKey}
if ($Check) {
Update-AzTag -ResourceId $resourceID -Tag $rgOldTag -Operation Delete
}
}
}
#Find Resources with oldKey, backup findings to CSV, migrate existing oldKey value to newKey merging with existing tags, then delete oldKey.
$resourcesOldKeyBackup = Get-AzResource | Where-Object {$_.Tags.Keys -match $oldKey}
$resourcesOldKeyBackup.count
if ($resourcesOldKeyBackup) {
Get-AzResource | Where-Object {$_.Tags.Keys -match $oldKey} | Out-File "C:tempAzResources-Tag-Backup-$oldkey.csv"
$resources = Get-AzResource | Where-Object {$_.Tags.Keys -match $oldKey}
$resources | ForEach-Object {
$resourcesOldKeyValue = $_.Tags.$oldKey
$resourcesNewTag = @{$newKey=$resourcesOldKeyValue}
$resourcesOldTag = @{$oldKey=$resourcesOldKeyValue}
$resourceID = $_.ResourceId
Update-AzTag -ResourceId $resourceID -Tag $resourcesNewTag -Operation Merge
$Check = Get-AzResource -ResourceId $resourceID | Where-Object {$_.Tags.Keys -match $newKey}
if ($Check) {
Update-AzTag -ResourceId $resourceID -Tag $resourcesOldTag -Operation Delete
}
}
}
Also, I would appreciate if you can help as well to make it more interactive and will ask to input an old key I am looking for and what will be the replacement instead of hardcoding the values in the script itself.
2
Answers
I Tried to reproduce the same in my environment to replace the Azure Tags using Powershell:
PowerShell Script:
Azure Tag Name and Value both are updated with new values.
Once ran the above commands Tags are updated in Resource Group.
Refer MS Doc more about Azure Tags using Powershell.
to search for resource groups with tag name or value equals to
xxx
:and then you just have to iterate over the result pretty much the same way you do. main advantage over your code – this scans all available subscriptions and does filtering on Azure side, so a lot quicker.