Forum Discussion
With Graph API we are only getting 1000 devices
- Aug 25, 2021
Ashish_Arya This script indeed does not add "paging" to retrieve additional devices if they exist. This is evident because it uses the "Get-Win10IntuneManagedDevice" function inside the script, which only does one call to the Graph API endpoint. You could potentially modify this function to do paging, on the other hand, there's a Graph PowerShell module nowadays which could potentially be used to replace parts of this script's custom code with simpler out of the box cmdlets.
The code responsible for only returning a subset is this:
else { $Resource = "deviceManagement/managedDevices?`$filter=(((deviceType%20eq%20%27desktop%27)%20or%20(deviceType%20eq%20%27windowsRT%27)%20or%20(deviceType%20eq%20%27winEmbedded%27)%20or%20(deviceType%20eq%20%27surfaceHub%27)))" $uri = "https://graph.microsoft.com/$graphApiVersion/$($Resource)" (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).value }
For more information on how paging works in Graph API, you can have a look at this URL.
pvanberlo : Thanks a lot for your reply. I have tried modifying the script and now I am getting the other devices as well.
But can you help me in understanding what does the value mean for the $resource variable.
Would you be happy to share your edited script to bring back all devices, I have tried adding the @odata.nextLink text to $Resource under the Get-Win10IntuneManagedDevice function but it still returns the first page of devices.
- Ashish_AryaOct 26, 2021Brass ContributorHi Rob,
Below is the edited Get-Win10IntuneManagedDevice function:
function Get-Win10IntuneManagedDevice {
<#
.SYNOPSIS
This gets information on Intune managed devices
.DESCRIPTION
This gets information on Intune managed devices
.EXAMPLE
Get-Win10IntuneManagedDevice
.NOTES
NAME: Get-Win10IntuneManagedDevice
#>
[cmdletbinding()]
param
(
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$deviceName
)
$graphApiVersion = "beta"
try {
if($deviceName){
$Resource = "deviceManagement/managedDevices?`$filter=deviceName eq '$deviceName'"
$uri = "https://graph.microsoft.com/$graphApiVersion/$($Resource)"
(Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).value
}
else {
$Resource = "deviceManagement/managedDevices?`$filter=(((deviceType%20eq%20%27desktop%27)%20or%20(deviceType%20eq%20%27windowsRT%27)%20or%20(deviceType%20eq%20%27winEmbedded%27)%20or%20(deviceType%20eq%20%27surfaceHub%27)))"
$uri = "https://graph.microsoft.com/$graphApiVersion/$($Resource)"
$DevicesResponse = (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get)
$Devices = $DevicesResponse.value
$DevicesNextLink = $DevicesResponse."@odata.nextLink"
while ($DevicesNextLink -ne $null){
$DevicesResponse = (Invoke-RestMethod -Uri $DevicesNextLink -Headers $authToken -Method Get)
$DevicesNextLink = $DevicesResponse."@odata.nextLink"
$Devices += $DevicesResponse.value
}
return $Devices
}
} catch {
$ex = $_.Exception
$errorResponse = $ex.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($errorResponse)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
Write-Host "Response content:`n$responseBody" -f Red
Write-Error "Request to $Uri failed with HTTP Status $($ex.Response.StatusCode) $($ex.Response.StatusDescription)"
throw "Get-IntuneManagedDevices error"
}
}- mjrogersAug 25, 2023Copper ContributorThanks so much, this is working perfectly.
- Ashish_AryaAug 26, 2023Brass Contributormjrogers You're welcome 😊.
- rob-collinsonNov 01, 2021Copper ContributorHi Ashish,
This is perfect, thanks very much- Ashish_AryaAug 26, 2023Brass Contributor