Skip to content

Commit 1b4b093

Browse files
committed
updates arg powershell pagination
1 parent fdceb2b commit 1b4b093

File tree

1 file changed

+91
-83
lines changed

1 file changed

+91
-83
lines changed

articles/governance/resource-graph/paginate-powershell.md

Lines changed: 91 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,142 @@
11
---
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
55
ms.topic: quickstart
66
ms.custom: dev-track-azurepowershell, devx-track-azurepowershell
77
---
8-
# Quickstart: Paginate Azure Resource Graph query results using Azure PowerShell
98

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
1310

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.
1612

1713
## Prerequisites
1814

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/).
2119

22-
## Add the Resource Graph module
20+
## Install the module
2321

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.
2823

29-
### Base requirements
24+
1. Verify your PowerShellGet version:
3025

31-
The Azure Resource Graph module requires the following software:
26+
```azurepowershell
27+
Get-Module -Name PowerShellGet
28+
```
3229
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).
3531
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:
3833
39-
### Install the module
34+
```azurepowershell
35+
Install-Module -Name Az.ResourceGraph -Repository PSGallery -Scope CurrentUser
36+
```
4037

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.
4239

43-
1. From a PowerShell prompt, run the following command:
40+
1. Verify the module was installed:
4441

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
4844
```
4945

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.
5147

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
5649

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.
5851

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
6354
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+
```
6559

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
6961

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.
7163

72-
```powershell
73-
# Login first with Connect-AzAccount if not using Cloud Shell
64+
The same query is used in each example:
7465

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+
```
8174

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:
8376

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+
```
8983

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.
9285

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"
9491
95-
while ($true) {
92+
$batchSize = 5
93+
$skipResult = 0
9694
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
10396
104-
$kqlResult += $graphResult.data
97+
while ($true) {
10598
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
110101
}
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+
```
112114

113115
## Clean up resources
114116

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:
117118

118-
```powershell
119-
# Remove the Resource Graph module from the current session
119+
```azurepowershell
120120
Remove-Module -Name Az.ResourceGraph
121+
```
122+
123+
To uninstall the `Az.ResourceGraph` module from your computer, run the following command:
121124

122-
# Uninstall the Resource Graph module from your computer
125+
```azurepowershell
123126
Uninstall-Module -Name Az.ResourceGraph
124127
```
125128

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+
126137
## Next steps
127138

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:
131140

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)
133142
- [Az.ResourceGraph PowerShell module reference](/powershell/module/az.resourcegraph)
134-
- [What is Azure Resource Graph?](overview.md)

0 commit comments

Comments
 (0)