Search code examples
powershellazuretagsazure-resource-group

Implementation of tags on resources in resource group using Powershell


I am trying to put tags on resources and resource group using Powershell. The problem is it remove existing tags and add the new tags.

Powershell Script :

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
}

What i want Suppose I have 3 tags. I will provide resource group and it should add these 3 tags to the resource group and its resources. The resource group and its resource already have some tags in it. I want to keep those tags as it is and if my new tags matches the existing ones then it should update it.

For only resource group

$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

Error

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

Add tags in Azure Resource Group Resources

$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
}

Error

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

Solution

  • If you use $tags_to_apply=(Get-AzureRmResourceGroup -Name $resourceGroupName).tags, the $tags_to_apply will be a hashtable.

    The resource group and its resource already have some tags in it. I want to keep those tags as it is and if my new tags matches the existing ones then it should update it.

    Per my understanding, you want to add new items in the hashtable, if the item is existing in the hashtable, update it. You could refer to my sample command, it works fine on my side.

    $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
    

    enter image description here

    Update:

    It may caused by a mix of int and string because of the key name, I change the key name, and it works fine.

    First, set the Tag.

    $resourceGroupName = "resourceGroupName"
    Set-AzureRmResourceGroup -Name $resourceGroupName -Tag @{tag1="one";tag2="two2";tag3="three"}
    

    Second, set the Tag with new hash table.

    $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
    

    enter image description here