使用Powershell在资源组中的资源上实现标记

使用Powershell在资源组中的资源上实现标记,powershell,azure,tags,azure-resource-group,Powershell,Azure,Tags,Azure Resource Group,我正在尝试使用Powershell在资源和资源组上放置标记。 问题是它删除了现有的标签并添加了新的标签 Powershell脚本: Get-AzureRmResourceGroup "testvsoRG" | Set-AzureRmResourceGroup -Tag @{Environment="UAT";Location="USA";DNS="www.xyz.com";Timezone="PST";Phase="Live"} $resourceGroupName="testvsoRG" $

我正在尝试使用Powershell在资源和资源组上放置标记。 问题是它删除了现有的标签并添加了新的标签

Powershell脚本

Get-AzureRmResourceGroup "testvsoRG" | Set-AzureRmResourceGroup -Tag @{Environment="UAT";Location="USA";DNS="www.xyz.com";Timezone="PST";Phase="Live"}

$resourceGroupName="testvsoRG"
$tags_to_apply=(Get-AzureRmResourceGroup -Name $resourceGroupName).tags
Find-AzureRmResource -ResourceGroupName $resourceGroupName | foreach 
{
    Set-AzureRmResource -ResourceId $_.resourceid -Tag $tags_to_apply -Force
}
我想要什么 假设我有3个标签。我将提供资源组,它应该将这3个标记添加到资源组及其资源中。资源组及其资源中已经有一些标记。我想保持这些标记的原样,如果我的新标记与现有标记匹配,那么它应该更新它

仅适用于资源组

$h = @{1="one";2="two";3="three"}
$resourceGroupName="testvsoRG"
$group=(Get-AzureRmResourceGroup -Name $resourceGroupName).tags
foreach ($key in $group.Keys)
{
    if ($h.ContainsKey($key))
    {
        $group.Remove($key)
    }
}
$group += $h
write-host $group
错误

System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.
Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry
Get-AzureRmResource : Parameter set cannot be resolved using the specified named parameters.
在Azure资源组资源中添加标记

$resources = Get-AzureRmResource -ResourceGroupName $resourceGroupName 
foreach ($r in $resources)
{
    $resourcetags = (Get-AzureRmResource -ResourceId $r.ResourceId).Tags    
    foreach ($rkey in $resourcetags.Keys)
    {
        if ($h.ContainsKey($rkey))
        {
            $resourcetags.Remove($rkey)
        }
    }
    $resourcetags += $h
    Set-AzureRmResource -Tag $resourcetags -ResourceId $r.ResourceId -Force
}
错误

System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.
Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry
Get-AzureRmResource : Parameter set cannot be resolved using the specified named parameters.

如果使用
$tags\u to\u apply=(Get-AzureRmResourceGroup-Name$resourceGroupName).tags
,则
$tags\u to\u apply
将是一个哈希表

资源组及其资源中已经有一些标记。我想保持这些标记的原样,如果我的新标记与现有标记匹配,那么它应该更新它

根据我的理解,您希望在哈希表中添加新项,如果该项存在于哈希表中,请更新它。您可以参考我的示例命令,它在我这边运行良好

$hashtable = @{1="one";2="two";3="three"}
$h = @{4="four";2="two2"}
foreach($key in $($hashtable.keys)){
    if ($h.ContainsKey($key)){
        $hashtable.Remove($key)
    }
}
$hashtable += $h

更新

它可能是由
int
string
的混合引起的,因为键名,我更改了键名,它工作正常

首先,设置
标记

$resourceGroupName = "resourceGroupName"
Set-AzureRmResourceGroup -Name $resourceGroupName -Tag @{tag1="one";tag2="two2";tag3="three"}
其次,使用新的哈希表设置
标记

$h = @{tag2="two";tag4="four"}
$resourceGroupName="resourceGroupName"
$group=(Get-AzureRmResourceGroup -Name $resourceGroupName).Tags
foreach($key in $($group.keys))
{
    if(!$key){
    if ($h.ContainsKey($key)){
        $group.Remove($key)
    }
   }
}
$group += $h
Set-AzureRmResourceGroup -Name $resourceGroupName -Tag $group

我是Powershell和azure的新手。我需要一段时间来实现这一点。让我们一个接一个地走吧。首先,我尝试只为资源组执行此操作,但它给出了错误。查看添加的代码。@rAJ您可以参考我的更新,任何问题都可以问。谢谢更新。当您必须添加新标记或更新现有标记,但它不保留现有标记时,它可以正常工作。我不想删除现有标记。@rAJ该命令不会删除现有标记,您能看到
1
3
仍然存在吗?它只是删除旧的现有的一个。