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