Sophos API pagination

I am exploring Sophos API to manage our organization. I am using powershell to query for Users not assigned to use group. The results returned are max of 100(I believe so). We have over 2k users. I would like to query ALL users in the organization. I understand that I should use pagination to return all users but can’t figure out the syntax. 

I have used “Do …While  “, which seemed to work for EndPoints, but doesn’t seem to return all pages for users.

do

{
 # Query for users 

$nextKey = $response.NextKey

} while ($nextKey -ne $null)

I have nullified $nextKey at the start

could someone help with code portion to address this issue? Or a proper way to incorporate the NextKey into the user query?



Added code
[edited by: Sophos User4238 at 10:11 PM (GMT -7) on 4 Nov 2023]
Parents
  • Here is my complete code if that would help. It returns only 50 users! HOW DO I GET IT TO RETURN ALL USERS?


    $userAPI = "">api-us03.central.sophos.com/.../users"
    $USAUserList = @()

    do {
    # Send a GET request to the API to retrieve a list of users
    $GetUsers = Invoke-RestMethod -Method Get -Uri $userAPI -Headers $USAheaders

    # Add the retrieved users to the array
    $USAUserList += $GetUsers.items

    # Check if there are more users to retrieve
    if ($GetUsers.next -ne $null) {
    $userAPI = $GetUsers.next
    } else {
    $NextKey = $null
    }
    } while ($GetUsers.next -ne $null)



    Added code
    [edited by: JBWANA at 10:13 PM (GMT -8) on 6 Nov 2023]
Reply
  • Here is my complete code if that would help. It returns only 50 users! HOW DO I GET IT TO RETURN ALL USERS?


    $userAPI = "">api-us03.central.sophos.com/.../users"
    $USAUserList = @()

    do {
    # Send a GET request to the API to retrieve a list of users
    $GetUsers = Invoke-RestMethod -Method Get -Uri $userAPI -Headers $USAheaders

    # Add the retrieved users to the array
    $USAUserList += $GetUsers.items

    # Check if there are more users to retrieve
    if ($GetUsers.next -ne $null) {
    $userAPI = $GetUsers.next
    } else {
    $NextKey = $null
    }
    } while ($GetUsers.next -ne $null)



    Added code
    [edited by: JBWANA at 10:13 PM (GMT -8) on 6 Nov 2023]
Children