Azure DevOps Rest Api可获取具有延续令牌的所有项目 [英] Azure DevOps Rest Api to get all projects with continuation token

查看:53
本文介绍了Azure DevOps Rest Api可获取具有延续令牌的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Azure DevOps Rest Api使用PowerShell在Azure DevOps中获取所有项目的列表.

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

但是,当我运行脚本时,它会继续返回100个项目.当我添加延续令牌时,它会循环并返回 SAME 100个项目4次.所以总共给我400个项目.我们目前有385个项目.

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

我希望看到$ projects.count等于我在组织中拥有的项目总数(以我为例)为385.我似乎无法理解我哪里出了问题以及为什么它会给我同样的100个项目重复使用延续令牌.

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.

推荐答案

仍然不确定为什么"do while" 循环不起作用,但是我只使用了 while strong>循环下面.

Still not sure why the "do while" loop doesnt work but i got it working using just a while loop below.


    $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 {

         $Uri="https://dev.azure.com/$Org/_apis/projects?continuationToken=$ContinuationToken&api-version=4"
         $ProjSets=Invoke-WebRequest -Uri $Uri -Method Get -ContentType "application/json" -Headers $header
         $continuationToken = $ProjSets.Headers.'x-ms-continuationtoken'
         $ProjectSet=$projsets.content | ConvertFrom-Json

         $projects=$ProjectSet.value.name

         while ($ContinuationToken -ne $null)
         {
         $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=$ProjSets.content | ConvertFrom-Json
         $projects += $ProjectSet.value.name
         $global:org_project_names = $projects
         write-host "Total number of projects = $($projects.count)"
        }
       }```

这篇关于Azure DevOps Rest Api可获取具有延续令牌的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆