Azure CLI – Filter information

Command line in general is a perfect way to automate. When looking at the Azure portal, it gives a great overview of all your resources. Sometimes, it is hard to find the information in the portal and especially when this information is spreaded over multiple instances.

This post is a start about querying information with the Azure CLI. You have to start simple to get great results. The query in this post is about tags. It is quite normal for a tag to have multiple values.

Overview of tags

When connection az to my Azure subscription, it is quite simple to get all information about the tags with the command ‘az tag list’.

Of course, it is not unreadable, but I’d like to see some thing like this.

TagValue
tf-testW11, lnx

It is not hard too find the query for the desired information. Command to be issued is
‘az tag list –query “[].{Tag: tagName, Value: TagValue[].tagValue}” –output table’. This command is not showing the output as desired. For some reason the values are not shown.

I expected the values to be in an array. The following commands showed me right.
az tag list –query “[].{Tag: tagName, Value: values[0].tagValue}” –output table
az tag list –query “[].{Tag: tagName, Value: values[1].tagValue}” –output table

So, the query is correct. That is also shown with the command when the output is in ‘normal’ json.
az tag list –query “[].{Tag: tagName, Value: values[].tagValue}

Normally I would say ‘Google is your best friend’, but in this case I was not able to find a correct solution. So I decided to give my syntax to Copilot and tell Copilot that the key part is missing from the output. I got a quite decent answer from Copilot.


To modify this command with the correct table headers, I ran the following command.
az tag list –query “[].{Tag:tagName, Key:join(‘, ‘, values[].tagValue)}” –output table

Two key takeaways for me, I learned something about querying the Azure cli. But most of all, I have a new (digital) best friend!


Tags:


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *