Azure DevOps Rest Api to get all projects with continuation token

Welcome Forums General PowerShell Q&A Azure DevOps Rest Api to get all projects with continuation token

This topic contains 1 reply, has 2 voices, and was last updated by

 
Senior Moderator
1 month ago.

  • Author
    Posts
  • #177790

    Participant
    Topics: 1
    Replies: 0
    Points: -8
    Rank: Member

    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.

  • #177885

    Senior Moderator
    Topics: 8
    Replies: 1085
    Points: 3,651
    Helping Hand
    Rank: Community Hero

You must be logged in to reply to this topic.