Basic Results Paging in Powershell with az graph

Basic Results Paging in Powershell with az graph

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
    }
    else{
        $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 $queryResult.data[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