我正在尝试使用Azure DevOps Rest Api使用PowerShell在Azure DevOps中获取所有项目的列表。
但是,当我运行脚本时,它会继续返回100个项目。当我添加延续令牌时,它会循环并返回 SAME 100个项目4次。所以总共给我400个项目。我们目前有385个项目。
$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个项目重复使用延续令牌。
答案 0 :(得分:0)
仍然不确定为什么“做一会儿” 循环不起作用,但是我仅使用下面的 while 循环来使它起作用。
$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)"
}
}```
答案 1 :(得分:0)
您可以使用$top
和continuationToken
对Azure Git Refs进行分页。这是文档:
https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/list?view=azure-devops-rest-6.0