问题
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.
回答1:
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)"
}
}```
回答2:
You can use $top
and continuationToken
for pagination of Azure Git Refs. Here is the documentation:
https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/list?view=azure-devops-rest-6.0
来源:https://stackoverflow.com/questions/57969982/azure-devops-rest-api-to-get-all-projects-with-continuation-token