Paging through non-paged results in MS Graph API

150 views Asked by At

I used to use a Microsoft Intune GraphAPI endpoint of:

https://graph.microsoft.com/beta/deviceAppManagement/mobileApps/$ApplicationID/devicestatuses

I used this in a PowerShell script, to get a list of devices that had a specific application/application ID installed.

This was in the Beta channel, but is no longer available. I have found I can use the following instead, as it is what the Intune portal uses to produce the same kind of on screen report:

https://graph.microsoft.com/beta/deviceManagement/reports/getDeviceInstallStatusReport

The issue I have with this compared to the old one, is that the old one had paginated responses, and we could submit multiple calls with the paging link to return all results. The new one I am using does not present server side pagination, so I am instead having to use the "top" and "skip" body properties to iterate through all records that exist.

I am finding that using this method using the code below, is very intermittent at returning all results (sometimes it does, sometimes it only returns a random portion of the results before failing):

$GraphAPIResource = "https://graph.microsoft.com/$graphApiVersion/deviceManagement/reports/getDeviceInstallStatusReport"

$Skip = 0
do {
    $body = @{
    select = @(
        "DeviceName"
        "UserPrincipalName"
        "InstallState"
        "DeviceId"
        "ErrorCode"
        "UserName"
        "UserId"
        "ApplicationId"
        "AppInstallState"
    )
    skip = $skip
    top = "50"
    filter = "(ApplicationId eq '$ApplicationID')"
    orderBy = @(
    )
}

$body = $body | ConvertTo-Json
$GraphAPIReturn = Invoke-RestMethod -Headers @{Authorization = "Bearer $($accesstoken)"} -Uri $GraphAPIResource -Method Post -Body $body -ContentType "application/json"

Write-Host $GraphAPIReturn
$TotalRowCount = $GraphAPIReturn.TotalRowCount
$Skip += 50
Start-Sleep -Milliseconds 500
} while (($Skip - 50) -le $TotalRowCount)

I put in a 500 millisecond delay, as I suspect there is some kind of throttling or rate limiting going on during my multiple disparate calls, but this did not help. I also tried upping the "top" property to 999, which I understood to be the max, although I think Intune has its own limit of 50 results.

  • Is there any reason my code would cause the behaviour I am seeing with intermittent reliability, or is it more likely that Microsoft are limiting my calls to the API because I am making too many separate calls in too short of a time?
  • Given this endpoint does not expose pagination, and I paging through the results in a supported/recommended way, or should I be using some other method to iterate through all pages?
  • Does anyone know of a different Intune Graph API endpoint that would give me the results I need but paginated?
0

There are 0 answers