Run Azure Resource Graph queries using Azure PowerShell
In my last article, I showed you how to enable the 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.
Connect-AzAccount
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:
Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"
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 'summarize count()'
Search-AzGraph `
-Query 'summarize count() by type'
Search-AzGraph `
-Query 'project name, type'
Search-AzGraph `
-Query "where type =~ 'microsoft.compute/virtualmachines' `
| project name, resourceGroup"
Search-AzGraph `
-Query "where type =~ 'microsoft.compute/virtualmachines' `
| project name, location, tags"
Search-AzGraph `
-Query "where type =~ 'microsoft.compute/virtualmachines' `
| project name | count"
Search-AzGraph `
-Query "where type =~ 'microsoft.compute/virtualmachines' `
| project name, location `
| summarize count() by location"
Search-AzGraph `
-Query "where type =~ 'microsoft.compute/virtualmachines' and name matches regex @'DEMO' `
| project name, location"