Azure DevOps Rest Api to get all projects with continuation token

I’m trying to get a list of all our projects in Azure DevOps with PowerShell using the Azure DevOps Rest Api.

However, when I run the script it keeps returning 100 projects. When I add the continuation token it loops and returns the SAME 100 projects 4 times. So giving me in total 400 projects. We currently have 385 projects.

 

    $Org = "ORGNAME" 
    $personalToken = "MYTOKEN"
    ###################################################
    Write-Host "Initialize authentication context" -ForegroundColor Yellow
    $token =[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))

    $header = @{authorization = "Basic $token"}
    $projects = $null


function get_projects {
    do
    {
        $uri="https://dev.azure.com/$Org/_apis/projects?continuationToken=$ContinuationToken&api-version=5.1"
        $ProjSets=Invoke-WebRequest -Uri $Uri -Method Get -ContentType "application/json" -Headers $header
        $continuationToken = $ProjSets.Headers.'x-ms-continuationtoken'
        $ProjectSet=$projset.content | ConvertFrom-Json
        $projects+=$ProjectSet.value
        }while ($continuationToken)
        write-host "$continuationToken" -ForegroundColor Cyan
        $projects.name
        $projects.count

}

get_projects

Im expecting to see $projects.count equal my total projects that I have in my org which in my case is 385. I can’t seem to understand where i’m going wrong and why it’s giving me the same 100 projects over and over again with the continuation token.

you may use $top filter criteria in the URL.

https://dev.azure.com/$Org/_apis/projects?continuationToken=$ContinuationToken&$top=385&api-version=5.1