Azure Graph rate limit solution with powershell
nconder opened this issue · comments
@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 :)
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
So I work in one of the top 5 largest Azure tenants in the world with well over 5k subs and discovered this a couple years ago.
To get around the resource limit I use skip and first.
Here is a PowerShell Graph-Pagination-Sample I use to pull storage account endpoints.