Basic Results Paging in Powershell with az graph

Basic Results Paging in Powershell with az graph

Omar McIver's photo
Omar McIver

Published on Sep 20, 2021

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Here is a short video with guidance on using az graph query paging in PowerShell, and one of the pitfalls to watch out for!

$query = "where location == 'centralus' order by id"

$skipToken = $null;
$queryResult = $null;

do {
    if ($null -eq $skipToken) {
        $queryResult = az graph query --graph-query $query --first 10 | ConvertFrom-Json
        $queryResult = az graph query --graph-query $query --skip-token $skipToken | ConvertFrom-Json
    $skipToken = $queryResult.skip_token;

    Write-Output "Page Count: $($queryResult.count)"
    Write-Output "Total Count: $($queryResult.total_records)"
    Write-Output $queryResult.skip_token
    Write-Output $[0].name

    # Don't run your activity against the resources here!! Instead, collect data to do this in a different loop.
    # If your activity takes time - you run the risk of getting inconsistent paging results on the next loop.

} while ($null -ne $skipToken)

# Create a new loop against collected information and do your processing here
Share this