Run Azure Resource Graph queries using Azure PowerShell

In my last article, I showed you how to enable Azure Resource Graph module in PowerShell and Azure CLI. Today I want to show you how to use this service to make queries and explore the resources using Azure PowerShell.

Requirements:

  • This tutorial assumes that you already have a Microsoft Azure account set up.
  • Have the Azure Resource Graph module enabled in Azure PowerShell, check out this link.

Azure PowerShell Workaround

If you want to know how to install the PowerShell Azure module on your machine, check out this link.

The simplest way to get started is to sign in interactively at the command line.

This cmdlet 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:

Once you set your default subscription, you’re ready to start.

Using the Search-AzGraph cmdlet.

To Run an Azure Resource Graph query, you should use the Search-AzGraph cmdlet with the following syntax.

The following query returns the number of Azure resources that exist in the subscriptions to which you have access.

Search-AzGraph -Query

We can modify the previous query to obtain the number of resources by type of resources.

Search-AzGraph -Query

We can also list all the resources of our subscription, using the following query.

Search-AzGraph -Query

Now that we know the types of resources that exist in our subscription, I will use one of them to make the queries, in my case I will use the virtual machines as an example of this guide.

This query returns the name and resource group properties of type resources: microsoft.compute / virtualmachines

Search-AzGraph -Query "where type"

Here another variant of the same type of query

Azure Resource Graph PowerShell

We can also add tabular operators to our queries, check out this link.

In this case, I will use the count operator.

Search-AzGraph -Query "where type"

In the following query, I will use the summarize operator.

Azure Resource Graph PowerShell

This query searches for virtual machines that match a regular expression (known as regex). Regex @ matches allow us to define the regular expression to match string or characters group.

Azure Resource Graph Regex

In the next post, I will show you how to run queries in your environment using Azure CLI.

If you want to know more about the Azure Resource Graph query language, check out this link:https://docs.microsoft.com/en-us/azure/governance/resource-graph/concepts/query-language