Home > Enterprise >  MS Graph API - How to query additional pages
MS Graph API - How to query additional pages

Time:10-08

I'm unsure as to how to query additional pages. Through some guides I've seen I've cobbled together the following but am only getting the first 1000 results (expecting a number close to 15000). I seem unable to capture the @odata.nextLink value to add to the loop.

$DeviceList = @()    
$URI = "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices?`$filter=contains(OperatingSystem,'iOS')&?`$select=serialNumber,Id"

if ($Null -ne $URI) 
    {
    $GetDevices = Invoke-RestMethod -Headers $Headers -Uri $URI
    $DeviceList  = $GetDevices.value
    $URI = $GetDevices.'@Odata.nextLink'
    }

$DeviceList | Export-Csv C:\Users\######\Downloads\Test.csv -Force -Append

CodePudding user response:

Assuming $uri.'@Odata.nextLink' actually has the URI for the next chunk you want to query, then you only need to change your if condition for a do \ while loop:

$uri = 'htts://graph.microsoft.com/.....'

& {
    do {
        $uri = Invoke-RestMethod -Headers $Headers -Uri $uri
        $uri.Value
        $uri = $uri.'@Odata.nextLink'
    } while($uri)
} | Export-Csv path\to\export.csv -NoTypeInformation
  • Related