Azure DevOps API is not returning all users - Only the First 498 users are returned

Regin Moses Jebaraj (external) 0 Reputation points
2024-05-22T12:01:58.0033333+00:00

The below code is fetching only the first 498 users. How to fetch all the users from Azure DevOps

Variables

$organization = ""

$pat = ""

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))

$apiVersion = "7.1-preview.1"

$users = @()

$url = "https://vssps.dev.azure.com/$organization/_apis/graph/users?api-version=$apiVersion"

$cnt = 1

Function to fetch users

function Get-Users {

param (

    [string]$url,

    [hashtable]$headers

)

$response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers

$global:users += $response.value



# Check for continuation token and fetch next page if it exists

if ($response.Headers.'x-ms-continuationtoken') {

    $continuationToken = $response.Headers.'x-ms-continuationtoken'

    $nextUrl = "https://vssps.dev.azure.com/$organization/_apis/graph/users?api-version=$apiVersion&continuationToken=$continuationToken"

    Get-Users -url $nextUrl -headers $headers

}

}

Initial headers

$headers = @{

Authorization = "Basic $base64AuthInfo"

}

Fetch all users

Get-Users -url $url -headers $headers

Output all users

$users | ForEach-Object {

Write-Output $_

Write-Output "User Cnt:" $cnt

$cnt = $cnt + 1

}

Write-host "Total Users - " $users.Count

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,230 questions
0 comments No comments
{count} votes