Skip to main content

Command Palette

Search for a command to run...

Basic Results Paging in Powershell with az graph

Published
1 min read
Basic Results Paging in Powershell with az graph
O

Omar McIver is a seasoned software architect turned AI transformation leader, with a passion for helping organizations and individuals harness the power of artificial intelligence. With years of experience in the tech industry and deep expertise in cloud-native technologies, Omar has evolved his focus to address the most critical challenge of our time: preparing the workforce for the AI revolution. As the Founder of Mode3 AI Training, Omar is pioneering a new approach to AI education that goes beyond simple tool usage. His company's innovative framework teaches professionals to partner with AI as "cybernetic teammates," achieving team-level performance as individuals while breaking down organizational silos.

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

More from this blog

O

Omar McIver ...a code blog

23 posts

Principal Cloud & AI Platform Architect at Mode3 Cloud, AI productivity guru, and family man balancing code and kids