1

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". :-/

lpacheco
  • 137
  • 1
  • 9

1 Answers1

2

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"
lpacheco
  • 137
  • 1
  • 9
Sam Cogan
  • 38,158
  • 6
  • 77
  • 113
  • Great! I didn't know about the Resource Graph. – lpacheco Feb 05 '20 at 17:02
  • This extension needs to be installed via `az extension add --name resource-graph`, otherwise you'll get the following error: `az: 'graph' is not in the 'az' command group`. – bart Jul 01 '20 at 19:05