'how to check if tags exists - azure policy

I'm trying to check if the tags do not exist in a resource. the tags are inserted from an array parameter named ExcludeTags, in the following format: Key;Value,Key;Value

this is the code:

{
    "count": {
        "value": "[parameters('ExcludeTags')]",
        "name": "tag",
        "where": {
            "field": "[format('tags[{0}]',split(current('tag'),';')[0])]",
            "equals": "[split(current('tag'),';')[1]]"
        }
    },
    "equals": 0
},

but when i upload the policy, i get the following error:

The policy definition 'the policy id' rule is invalid. The 'field' property value '[format('tags[{0}]',split(current('tag'),';')[0])]' is invalid. 'field' and 'current' functions are not supported in the 'field' accessor.

does anyone know a better way to do that?

thank you



Solution 1:[1]

  • There are two commands for applying tags New-AzTag and Update-azTag through PowerShell. you can replace all tags on the resource group, subscription using New-AzTag

You can apply set of tags to your storage account using given command:

$tags = @{"Dept"="Finance"; "Status"="Normal"}
$resource = Get-AzResource -Name demoStorage -ResourceGroup demoGroup
New-AzTag -ResourceId $resource.id -Tag $tags

  • If you run your command with different tags than erlier tag will remove.
$tags = @{"Team"="Compliance"; "Environment"="Production"}
New-AzTag -ResourceId $resource.id -Tag $tags

  • For adding tags to those resources which have tags already.
$tags = @{"Dept"="Finance"; "Status"="Normal"}
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge

  • The same commands also work with resource groups or subscriptions. You pass in the identifier for the resource group or subscription you want to tag.
  • To add a new set of tags to a resource group, use:
    $tags = @{"Dept"="Finance"; "Status"="Normal"}
    $resourceGroup = Get-AzResourceGroup -Name demoGroup
    New-AzTag -ResourceId $resourceGroup.ResourceId -tag $tags

For complete information you can check this Microsoft Document.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SaiSakethGuduru-MT