Azure CLI – Finding Azure Resources Missing a Specific Tag

azureazure-cli

I need to assure that all our resource groups have a specific tag.

I know I can use a policy to ensure that any created resource will have the tag, but for existing resources I'm trying to come up with a query using AZ CLI.

As an extra challenge, the tag has a space in the middle of its name: it is "Cost Center" instead of "CostCenter". :-/

Best Answer

I would recommend using Azure Resource Graph for this. Resource graph allows you to query all your Azure resources using the Kusto language either in the CLI or in the portal.

To look for the name of all resource groups that do not have the "Cost Center" tag you could run a query like:

ResourceContainers 
| project name, type, tags 
| where type == 'microsoft.resources/subscriptions/resourcegroups' 
| where tags !contains 'Cost Center' 
| project name

From the CLI this query would look like:

az graph query -q "ResourceContainers | project name, type, tags | where type == 'microsoft.resources/subscriptions/resourcegroups' | where tags !contains 'Cost Center' | project name"
Related Topic