|
| 1 | +--- |
| 2 | +title: 'Paginate Azure Resource Graph query results using Azure PowerShell' |
| 3 | +description: In this quickstart, you control the volume Azure Resource Graph query output by using pagination in Azure PowerShell. |
| 4 | +ms.date: 11/11/2022 |
| 5 | +ms.topic: quickstart |
| 6 | +ms.author: timwarner |
| 7 | +author: timwarner-msft |
| 8 | +ms.custom: dev-track-azurepowershell |
| 9 | +--- |
| 10 | +# Quickstart: Paginate Azure Resource Graph query results using Azure PowerShell |
| 11 | + |
| 12 | +By default, Azure Resource Graph returns a maximum of 1000 records for each query. However, you can |
| 13 | +use the *Search-AzGraph* cmdlet's `skipToken` parameter to adjust how many records you return per |
| 14 | +request. |
| 15 | + |
| 16 | +At the end of this quickstart, you'll be able to customize the output volume returned by your Azure Resource |
| 17 | +Graph queries by using Azure PowerShell. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account |
| 22 | +before you begin. |
| 23 | + |
| 24 | +## Add the Resource Graph module |
| 25 | + |
| 26 | +To enable Azure PowerShell to query Azure Resource Graph, the **Az.ResourceGraph** module must be |
| 27 | +added. This module can be used with locally installed PowerShell, with |
| 28 | +[Azure Cloud Shell](https://shell.azure.com), or with the |
| 29 | +[PowerShell Docker image](https://hub.docker.com/_/microsoft-powershell). |
| 30 | + |
| 31 | +### Base requirements |
| 32 | + |
| 33 | +The Azure Resource Graph module requires the following software: |
| 34 | + |
| 35 | +- Azure PowerShell 8.x or higher. If it isn't yet installed, follow |
| 36 | + [these instructions](/powershell/azure/install-az-ps). |
| 37 | + |
| 38 | +- PowerShellGet 2.0.1 or higher. If it isn't installed or updated, follow |
| 39 | + [these instructions](/powershell/scripting/gallery/installing-psget). |
| 40 | + |
| 41 | +### Install the module |
| 42 | + |
| 43 | +The Resource Graph module for PowerShell is **Az.ResourceGraph**. |
| 44 | + |
| 45 | +1. From a PowerShell prompt, run the following command: |
| 46 | + |
| 47 | + ```powershell |
| 48 | + # Install the Resource Graph module from PowerShell Gallery |
| 49 | + Install-Module -Name Az.ResourceGraph -Scope CurrentUser -Repository PSGallery -Force |
| 50 | + ``` |
| 51 | + |
| 52 | +1. Validate that the module has been imported and is at least version `0.11.0`: |
| 53 | + |
| 54 | + ```powershell |
| 55 | + # Get a list of commands for the imported Az.ResourceGraph module |
| 56 | + Get-Command -Module Az.ResourceGraph |
| 57 | + ``` |
| 58 | + |
| 59 | +## Paginate Azure Resource Graph query results |
| 60 | + |
| 61 | +With the Azure PowerShell module added to your environment of choice, it's time to try out a simple |
| 62 | +tenant-based Resource Graph query and work with paginating the results. We'll start with an ARG |
| 63 | +query that returns a list of all virtual machines (VMS) across all subscriptions associated with a |
| 64 | +given Azure Active Directory (Azure AD) tenant. |
| 65 | + |
| 66 | +We'll then configure the query to return five records (VMs) at a time. |
| 67 | + |
| 68 | +> [!NOTE] |
| 69 | +> This example query is adapted from the work of Microsoft Most Valuable Professional (MVP) |
| 70 | +> [Oliver Mossec](https://github.com/omiossec). |
| 71 | +
|
| 72 | +1. Run the initial Azure Resource Graph query using the `Search-AzGraph` cmdlet: |
| 73 | + |
| 74 | + ```powershell |
| 75 | + # Login first with Connect-AzAccount if not using Cloud Shell |
| 76 | +
|
| 77 | + # Run Azure Resource Graph query Search-AzGraph -Query "Resources | join kind=leftouter |
| 78 | + (ResourceContainers | where type=='microsoft.resources/subscriptions' | project subscriptionName |
| 79 | + = name, subscriptionId) on subscriptionId | where type =~ 'Microsoft.Compute/virtualMachines' | |
| 80 | + project VMResourceId = id, subscriptionName, resourceGroup, name" |
| 81 | + ``` |
| 82 | + |
| 83 | +1. Update the query to implement the `skipToken` parameter and return 5 VMs in each batch: |
| 84 | + |
| 85 | + ```powershell |
| 86 | + $kqlQuery = "Resources | join kind=leftouter (ResourceContainers | where |
| 87 | + type=='microsoft.resources/subscriptions' | project subscriptionName = name,subscriptionId) on |
| 88 | + subscriptionId | where type =~ 'Microsoft.Compute/virtualMachines' | project VMResourceId = id, |
| 89 | + subscriptionName, resourceGroup,name" |
| 90 | +
|
| 91 | + $batchSize = 5 |
| 92 | + $skipResult = 0 |
| 93 | +
|
| 94 | + [System.Collections.Generic.List[string]]$kqlResult |
| 95 | +
|
| 96 | + while ($true) { |
| 97 | +
|
| 98 | + if ($skipResult -gt 0) { |
| 99 | + $graphResult = Search-AzGraph -Query $kqlQuery -First $batchSize -SkipToken $graphResult.SkipToken |
| 100 | + } |
| 101 | + else { |
| 102 | + $graphResult = Search-AzGraph -Query $kqlQuery -First $batchSize |
| 103 | + } |
| 104 | +
|
| 105 | + $kqlResult += $graphResult.data |
| 106 | +
|
| 107 | + if ($graphResult.data.Count -lt $batchSize) { |
| 108 | + break; |
| 109 | + } |
| 110 | + $skipResult += $skipResult + $batchSize |
| 111 | + } |
| 112 | + ``` |
| 113 | + |
| 114 | +## Clean up resources |
| 115 | + |
| 116 | +If you wish to remove the Resource Graph module from your Azure PowerShell environment, you can do |
| 117 | +so by using the following command: |
| 118 | + |
| 119 | +```powershell |
| 120 | +# Remove the Resource Graph module from the current session |
| 121 | +Remove-Module -Name Az.ResourceGraph |
| 122 | +
|
| 123 | +# Uninstall the Resource Graph module from your computer |
| 124 | +Uninstall-Module -Name Az.ResourceGraph |
| 125 | +``` |
| 126 | + |
| 127 | +## Next steps |
| 128 | + |
| 129 | +In this quickstart, you learned how to paginate Azure Resource Graph query results by using |
| 130 | +Azure PowerShell. To learn more about the Resource Graph language, review any of the following |
| 131 | +Microsoft Learn resources. |
| 132 | + |
| 133 | +- [Work with large data sets - Azure Resource Graph](concepts/work-with-data.md) |
| 134 | +- [Az.ResourceGraph PowerShell module reference](/powershell/module/az.resourcegraph) |
| 135 | +- [What is Azure Resource Graph?](overview.md) |
0 commit comments