SOLVED

With Graph API we are only getting 1000 devices

%3CLINGO-SUB%20id%3D%22lingo-sub-2682887%22%20slang%3D%22en-US%22%3EWith%20Graph%20API%20we%20are%20only%20getting%201000%20devices%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2682887%22%20slang%3D%22en-US%22%3E%3CP%3EHI%20Team%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20are%20using%20the%20below%20PowerShell%20script%20to%20change%20the%20Primary%20user%20of%20a%20device%20by%20checking%20the%20last%20logged%20in%20userid.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20is%20the%20github%20repo%20link%20which%20holds%20this%20PowerShell%20script%20and%20also%20the%20link%20of%20an%20article%20about%20the%20explanation%20of%20this%20script%20-%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fraw.githubusercontent.com%2Fsvdbusse%2FIntuneScripts%2Fmaster%2FPrimaryUser%2FSet-PrimaryUserfromLastLogIn.ps1%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fraw.githubusercontent.com%2Fsvdbusse%2FIntuneScripts%2Fmaster%2FPrimaryUser%2FSet-PrimaryUserfromLastLogIn.ps1%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fsvdbusse.github.io%2FSemiAnnualChat%2F2020%2F03%2F21%2FChanging-Intune-Primary-User-To-Last-Logged-On-User.html%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fsvdbusse.github.io%2FSemiAnnualChat%2F2020%2F03%2F21%2FChanging-Intune-Primary-User-To-Last-Logged-On-User.html%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20problem%20now%20is%20that%20we%20are%20only%20able%20to%20get%201000%20devices%20in%20the%20%24Devices%20variable%20in%20the%20above%20mentioned%20script%20and%20we%20have%20around%202000%20devices%20so%201000%20more%20devices%20are%20not%20getting%20fetched%20by%20this%20script.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%20this%20script%20always%20get%20the%20device%20in%20the%20same%20pattern%20i.e..%20if%20I%20run%20the%20script%20today%20and%20tomorrow%20then%20the%20devices%20will%20show%20the%20same%20pattern%20that%20is%20also%20the%20reason%20the%20rest%201000%20devices%20are%20not%20getting%20fetched.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20solution%20to%20this%20issue%20will%20be%20a%20great%20help%20for%20me.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%2C%3C%2FP%3E%3CP%3EAshish%20Arya%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2682887%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EGraph%20API%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMicrosoft%20Intune%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMobile%20Device%20Management%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowershell%20and%20Rest%20API%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epowershell%20script%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2683001%22%20slang%3D%22en-US%22%3ERe%3A%20With%20Graph%20API%20we%20are%20only%20getting%201000%20devices%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2683001%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F384543%22%20target%3D%22_blank%22%3E%40Ashish_Arya%3C%2FA%3E%26nbsp%3BThis%20script%20indeed%20does%20not%20add%20%22paging%22%20to%20retrieve%20additional%20devices%20if%20they%20exist.%20This%20is%20evident%20because%20it%20uses%20the%20%22Get-Win10IntuneManagedDevice%22%20function%20inside%20the%20script%2C%20which%20only%20does%20one%20call%20to%20the%20Graph%20API%20endpoint.%20You%20could%20potentially%20modify%20this%20function%20to%20do%20paging%2C%20on%20the%20other%20hand%2C%20there's%20a%20Graph%20PowerShell%20module%20nowadays%20which%20could%20potentially%20be%20used%20to%20replace%20parts%20of%20this%20script's%20custom%20code%20with%20simpler%20out%20of%20the%20box%20cmdlets.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%20responsible%20for%20only%20returning%20a%20subset%20is%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%20%20%20%20%20%20%20else%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%24Resource%20%3D%20%22deviceManagement%2FmanagedDevices%3F%60%24filter%3D(((deviceType%2520eq%2520%2527desktop%2527)%2520or%2520(deviceType%2520eq%2520%2527windowsRT%2527)%2520or%2520(deviceType%2520eq%2520%2527winEmbedded%2527)%2520or%2520(deviceType%2520eq%2520%2527surfaceHub%2527)))%22%0A%20%20%20%20%20%20%20%20%20%24uri%20%3D%20%22https%3A%2F%2Fgraph.microsoft.com%2F%24graphApiVersion%2F%24(%24Resource)%22%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20(Invoke-RestMethod%20-Uri%20%24uri%20-Headers%20%24authToken%20-Method%20Get).value%0A%0A%20%20%20%20%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20more%20information%20on%20how%20paging%20works%20in%20Graph%20API%2C%20you%20can%20have%20a%20look%20at%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fpaging%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ethis%3C%2FA%3E%20URL.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2699509%22%20slang%3D%22en-US%22%3ERe%3A%20With%20Graph%20API%20we%20are%20only%20getting%201000%20devices%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2699509%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F384543%22%20target%3D%22_blank%22%3E%40Ashish_Arya%3C%2FA%3E%26nbsp%3BThat's%20the%20specific%20API%20that's%20being%20called%2C%20in%20this%20case%20the%20'%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fapi%2Fintune-devices-manageddevice-list%3Fview%3Dgraph-rest-1.0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EdeviceManagement%2FmanagedDevices%3C%2FA%3E'%20one%2C%20which%20returns%20all%20managed%20devices%20in%20your%20tenant.%20It%20adds%20a%20filter%2C%20specifically%20to%20only%20return%20devices%20based%20on%20the%20'deviceType'%20attribute%2C%20which%20must%20contain%20the%20value%20'desktop'%2C%20'windowsRT'%2C%20'winEmbedded'%20or%20'surfaceHub'%20for%20a%20device%20to%20be%20returned%20in%20this%20API%20call.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2715908%22%20slang%3D%22en-US%22%3ERe%3A%20With%20Graph%20API%20we%20are%20only%20getting%201000%20devices%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2715908%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3BThank%20you%20so%20much%20for%20the%20help.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2870020%22%20slang%3D%22en-US%22%3ERe%3A%20With%20Graph%20API%20we%20are%20only%20getting%201000%20devices%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2870020%22%20slang%3D%22en-US%22%3EHi%20Ashish%2C%3CBR%20%2F%3E%3CBR%20%2F%3EWould%20you%20be%20happy%20to%20share%20your%20edited%20script%20to%20bring%20back%20all%20devices%2C%20I%20have%20tried%20adding%20the%20%40odata.nextLink%20text%20to%20%24Resource%20under%20the%20Get-Win10IntuneManagedDevice%20function%20but%20it%20still%20returns%20the%20first%20page%20of%20devices.%3C%2FLINGO-BODY%3E
Occasional Contributor

HI Team,

 

We are using the below PowerShell script to change the Primary user of a device by checking the last logged in userid.

 

Below is the github repo link which holds this PowerShell script and also the link of an article about the explanation of this script - 

 

https://raw.githubusercontent.com/svdbusse/IntuneScripts/master/PrimaryUser/Set-PrimaryUserfromLastL...

 

https://svdbusse.github.io/SemiAnnualChat/2020/03/21/Changing-Intune-Primary-User-To-Last-Logged-On-...

 

The problem now is that we are only able to get 1000 devices in the $Devices variable in the above mentioned script and we have around 2000 devices so 1000 more devices are not getting fetched by this script.

 

Also this script always get the device in the same pattern i.e.. if I run the script today and tomorrow then the devices will show the same pattern that is also the reason the rest 1000 devices are not getting fetched.

 

Any solution to this issue will be a great help for me. 

 

Regards,

Ashish Arya

 

 

7 Replies
best response confirmed by Ashish_Arya (Occasional Contributor)
Solution

@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.

 

 $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)))"
 
 

@Ashish_Arya That's the specific API that's being called, in this case the 'deviceManagement/managedDevices' one, which returns all managed devices in your tenant. It adds a filter, specifically to only return devices based on the 'deviceType' attribute, which must contain the value 'desktop', 'windowsRT', 'winEmbedded' or 'surfaceHub' for a device to be returned in this API call.

@pvanberlo Thank you so much for the help.

Hi Ashish,

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.
Hi 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"
}
}
Hi Ashish,

This is perfect, thanks very much