更新 Azure 资源的标签

huangapple go评论50阅读模式
英文:

Update Azure tags of resources

问题

我是初学PowerShell的,请谅解。我正在编写一个脚本,其中需要替换特定订阅中任何资源的标记的一个键值对,可以是标记的“名称”或“值”。

我有一个脚本,可以查找旧的标记键并将其替换为新的标记键。它会检查所有资源,找到旧标记并成功替换为新标记。现在,我的困惑是它只能处理标记的“名称”部分,而不能处理标记的“值”部分。请问有人可以帮助我如何处理标记的“值”部分吗?

总之,此代码将搜索* $oldKey* 在所有资源中替换为* $newKey* 的标记。它会创建一个报告,记录所做的更改,并将其放入CSV文件。

# 定义旧标记键 $oldkey 和新标记键 $newKey
$oldKey = "camp"
$newKey = "Camp3"

# 查找具有 oldKey 的 ResourceGroups,备份结果到 CSV,将现有的 oldKey 值迁移到 newKey 并合并到现有标记,然后删除 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:\temp\AzRGs-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
        }
    }   
}

# 查找具有 oldKey 的资源,备份结果到 CSV,将现有的 oldKey 值迁移到 newKey 并合并到现有标记,然后删除 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:\temp\AzResources-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
        }
    }
}

此外,我会感激如果您可以帮助使其更加交互,并要求输入我要查找的旧键和要替代的内容,而不是在脚本中硬编码这些值。

英文:

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:\temp\AzRGs-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:\temp\AzResources-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.

答案1

得分: 0

我尝试在我的环境中使用 Powershell 替换 Azure 标签,以下是 Powershell 脚本部分的翻译:

connect-azaccount
$oldTagKey = "OldTagKey"
$oldTagValue = "OldTagValue"
$replacedTags= {"NewTagKey"="NewTagValue"}
#$resourcelist= Get-AzResource
$resources = Get-AzResourceGroup | Where-Object {$_.Tags.Keys -match "OldTagKey";}
foreach($item in $resources){
# 获取资源的当前标签
    $tags = $item.Tags
    if ($tags.ContainsKey($oldTagKey)) {
# 使用新标签键和值更新标签
        $tags["NewTagKey"] = "NewTagValue"
        # 移除旧标签
        $tags.Remove($oldTagKey)
        Update-Aztag -ResourceId $item.ResourceId -Tag $replacedTags -Operation Replace  
    }
}

Azure 标签名称 都已更新为新值。

运行上述命令后,标签 已在 资源组 中更新。

更多关于使用 Powershell 处理 Azure 标签 的信息,请参考 MS Doc

英文:

I Tried to reproduce the same in my environment to replace the Azure Tags using Powershell:

PowerShell Script:

   connect-azaccount
   $oldTagKey = "OldTagKey"
   $oldTagValue = "OldTagValue"
   $replacedTags= @{"NewTagKey"="NewTagValue"}
    #$resourcelist= Get-AzResource
    $resources = Get-AzResourceGroup | Where-Object {$_.Tags.Keys -match "OldTagKey"}
    foreach($item in $resources){
   # Get the current tags for the resource
        $tags = $item.Tags
        if ($tags.ContainsKey($oldTagKey)) {
  # Update the tag with the new tag key and value
        $tags[$newTagKey] = $newTagValue
        # Remove the old tag
        $tags.Remove($oldTagKey)
        Update-Aztag -ResourceId $item.ResourceId -Tag $replacedTags -Operation Replace  
        }
}

Azure Tag Name and Value both are updated with new values.

更新 Azure 资源的标签

Once ran the above commands Tags are updated in Resource Group.

更新 Azure 资源的标签

Refer MS Doc more about Azure Tags using Powershell.

答案2

得分: 0

搜索标签名称或值等于xxx的资源组:

$searchPattern = 'xxx'
$resourceGroups = Search-AzGraph "ResourceContainers | where tags contains '$searchPattern' and type =~ 'microsoft.resources/subscriptions/resourcegroups'"

然后,你只需要以几乎与你之前相同的方式迭代结果。与你的代码相比的主要优点是,这会扫描所有可用的订阅,并在 Azure 侧进行过滤,因此速度更快。

英文:

to search for resource groups with tag name or value equals to xxx:

$searchPattern = 'xxx'
$resourceGroups = Search-AzGraph "ResourceContainers | where tags contains '$searchPattern' and type =~ 'microsoft.resources/subscriptions/resourcegroups'"

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.

huangapple
  • 本文由 发表于 2023年2月10日 05:31:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定