When working with Microsoft Graph API via PowerShell, particularly in environments with excessive volumes of requests, you would possibly encounter throttling limits.
Throttling occurs when too many requests are despatched to the API inside a short while body, leading to a Too Many Requests response or Too many retries carried out error. That is Microsoft’s method of managing the load on their servers and guaranteeing honest utilization throughout all purchasers.
To deal with throttling successfully, it’s essential to implement retry logic in your PowerShell scripts utilizing the Graph SDK.
This weblog put up will information you thru a easy method to managing throttling in your PowerShell scripts.
We lately encountered a problem whereas trying to retrieve over 100,000 information utilizing the Microsoft Graph API with the PowerShell Graph SDK.
The API began failing after processing about 15 pages, returning the next error:
Invoke-MgGraphRequest -Technique Get -Uri $apiUrl
$apiUrl = ‘https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations/’; + $policyId + ‘/deviceStatuses’

After conducting some analysis, we discovered an answer to this downside. By including the next command after establishing the connection to Microsoft Graph, we are able to mitigate the throttling points:
Join-MgGraph -ClientId $MgGClientID -CertificateThumbprint $ThumbPrint -TenantId $TenantName -NoWelcome # connection command
To keep away from throttling, embody the next command to set the request context:
Set-MgRequestContext -MaxRetry 5 -RetryDelay 10
This configuration ensures that our requests are robotically retried as much as 5 occasions with a delay of ten seconds between every try, considerably decreasing the chance of encountering throttling errors.
Thanks for studying …
Tech Wizard