azure - 使用powershell覆盖天蓝色资源组上的标签名称
问题描述
下面是天蓝色的资源
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
从上面的输出中,我想将标记名称“分类”替换为“分类”,将“公共”替换为“帐户”。
我正在按照以下程序执行此操作。
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
。然后,您可以使用 修改新更新的哈希表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
操作将更新值,但不会更新键名。
推荐阅读
- sorting - 如何在select2中按字母顺序进行选定和未选定排序?
- react-navigation - route.params 在 useFocusEffect() 中不可见
- sql - 将 SQL 代码(作为字符串)转换为表 - 动态数据透视
- firebase - NoSuchMethodError。getter 'weekday' 被调用为 null。Table_Calendar Flutter
- javascript - 基于 state.vallue 值 reactjs 隐藏/取消隐藏操作图标
- python-3.x - Spyder,干净的 Win 安装,版本。4.2.3 - pip 给出消息“没有名为 pip 的模块”
- php - 如何使包含转义序列和反斜杠的json有效?
- php - PHP Regex 忽略注释行
- ios - 我的导航栏和警报标题以及底部警报标题在整个项目中变为白色
- python - 如何在光线追踪迷宫之前运行动画屏幕(python asciimatics)