gpt4 book ai didi

azure - 使用 powershell 覆盖 azure 资源组上的标签名称

转载 作者:行者123 更新时间:2023-12-03 02:33:37 26 4
gpt4 key购买 nike

下面是 azure 资源

PS C:\Windows\System32> Get-AzResource -ResourceGroupName paniRG


Name : paniavset123
ResourceGroupName : paniRG
ResourceType : Microsoft.Compute/availabilitySets
Location : eastus
ResourceId : /subscriptions/8987b447-d083-481e-9c0f-f2b73a15b18b/resourceGroups/paniRG/providers/Microsoft.C
ompute/availabilitySets/paniavset123
Tags :
Name Value
============== ======
CostCenter ABCDEG
Owner john
classification Public

从上面的输出中,我想将标签名称“classification”替换为“Classification”,将“Public”替换为“Accounts”。

我按照以下过程来执行此操作。

PS C:\Windows\System32> $tags=@{"Classification"="Accounts"}

PS C:\Windows\System32> $s=Get-AzResource -ResourceGroupName paniRG

PS C:\Windows\System32> Update-AzTag -ResourceId $s.ResourceId -Tag $tags -Operation Merge


Id : /subscriptions/8987b447-d083-481e-9c0f-f2b73a15b18b/resourceGroups/paniRG/providers/Microsoft.Compute/availabilitySets/
paniavset123/providers/Microsoft.Resources/tags/default
Name : default
Type : Microsoft.Resources/tags
Properties :
Name Value
============== ========
CostCenter ABCDEG
Owner john
classification Accounts

当我使用 update-aztag 命令时,标签值正在更改,但名称仍显示为“分类”。

有人可以帮我解决这个问题吗?

最佳答案

您可以删除 classification来自哈希表资源的标签 Tags ,然后添加新的 Classification标记为新的 Account值(value)。然后,您可以使用 Set-AzResource 修改新更新的哈希表。 .

$resource = Get-AzResource -Name "RESOURCE NAME" -ResourceGroupName "RESOURCE GROUP NAME"

# Get the tags
$tags = $resource.Tags

# Check if hashtable has classification key
if ($tags.ContainsKey("classification")) {

# Remove classification key if it exists
$tags.Remove("classification")
}

# Create Classification tag with Accounts value
$tags.Classification = "Accounts"

# Update resource with new tags
$resource | Set-AzResource -Tag $tags -Force

如果您想使用 Update-AzTag ,您将需要使用 Replace操作:

Update-AzTag -ResourceId $s.Id -Tag $tags -Operation Replace

Merge操作将更新值,但不会更新键名称。

关于azure - 使用 powershell 覆盖 azure 资源组上的标签名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64089274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com