Run Azure Resource Graph queries using Azure CLI
To end this series of articles about Azure Resource Graph. In this last article, I want to show you how to use this service to make inquiries and explore resources with the Azure CLI. Requirements:
- This tutorial assumes that you already have a Microsoft Azure account set up.
- Add the Azure Resource Graph extension to your Azure CLI environment, check out this link.
Azure CLI Workaround #
You can use it in your browser with Azure Cloud Shell or install it on your machine. If you want to know how to install the Azure CLI, check out this link. The way to get started is to sign in interactively at the command line.
az login
This command will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. to perform this task, we will use the following commands:
az account list
az account set \
--subscription "Subscription Name"
Once you set your default subscription, you’re ready to start.
Azure Resource Graph queries #
To Run an Azure Resource Graph query, you should use the graph extension and query command with the following syntax. The following query returns the number of Azure resources that exist in the subscriptions to which you have access.
az graph query \
-q "summarize count()" \
-o table
az graph query \
-q "summarize count() by type" \
--query "[].{Type:type,Count:count_}" \
-o table
az graph query \
-q "project name, type" \
--query "[].{Name:name, Type:type}" \
-o table
az graph query \
-q "where type =~ 'microsoft.compute/virtualmachines' | project name, resourceGroup" \
-o table
az graph query \
-q "where type =~ 'microsoft.compute/virtualmachines' | project name, location, tags" \
--query "[].{Name:name, Location:location, Tags:tags.OS}" \
-o table
az graph query \
-q "where type =~ 'microsoft.compute/virtualmachines' | project name | count" \
-o table
az graph query \
-q "where type =~ 'microsoft.compute/virtualmachines' | project name, location | summarize count() by location" \
-o table
az graph query \
-q "where type =~ 'microsoft.compute/virtualmachines' and name matches regex @'DEMO'| project name, location |order by name asc" \
-o table"