dirkjanm / ROADtools

A collection of Azure AD/Entra tools for offensive and defensive security purposes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Azure Graph rate limit solution with powershell

nconder opened this issue · comments

commented

@dirkjanm this a snip of what I use in powershell to get around graph rate limits on subscription and resources. A short one or two second pause should be worked into the loop so graph is not taxed.

`# Fetch the full array of subscription IDs
$subscriptions = Get-AzSubscription
$subscriptionIds = $subscriptions.Id

query

$query = "resources | order by subscriptionId asc";

Create a subscription counter, set the batch size, and prepare a variable for the results

$counter = [PSCustomObject] @{ Value = 0 }
$batchSize = 1000

Create array to hold results

$response = @()

Group the subscriptions into batches

$subscriptionsBatch = $subscriptionIds | Group -Property { [math]::Floor($counter.Value++ / $batchSize) }

Run the query for each batch

foreach ($batch in $subscriptionsBatch)
{

Create a resource counter, set the batch size, and prepare a variable for the results

$Skip = 0;
$First = 1000;

Get the data

$response += do {if ($Skip -eq 0) {$y = Search-AzGraph -Query $query -First $First -Subscription $batch.Group ; }
else {$y = Search-AzGraph -Query $query -Skip $Skip -First $First -Subscription $batch.Group } $cont = $y.Count -eq $First; $Skip = $Skip + $First; $y; } while ($cont) }

thx, latest commit on master uses a combination of token buckets and automatic throttling detection to work around this :)

commented

That snip also gets past the hard limit on resources and subscriptions that can be retrieved via Graph.

do you have a reference on what those hard limits are? haven't run into them yet