Forum Discussion

Ashish_Arya's avatar
Ashish_Arya
Brass Contributor
Aug 25, 2021
Solved

With Graph API we are only getting 1000 devices

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-PrimaryUserfromLastLogIn.ps1

 

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

 

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

 

 

  • 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's avatar
    pvanberlo
    Steel Contributor

    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.

    • Ashish_Arya's avatar
      Ashish_Arya
      Brass Contributor

      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)))"
       
       
      • rob-collinson's avatar
        rob-collinson
        Copper Contributor
        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.

Resources