I'm creating a powershell script that connects to the Azure Devops API service to get all the projects, teams and team members. However I'm having some issues with the pagination of the teams, since the API only returns 1000 results. I'm usig the top and skip parameters of the query to sort this out however this is not working on my script, here's what I have:
$Members = @()
$Organization = 'org'
$PAT = 'pat'
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
$UriOrganization = "https://dev.azure.com/$($Organization)/"
$top_param=1000
$skip_param=0
$uriProject = $UriOrganization + "_apis/projects"
$ProjectsResult = Invoke-RestMethod -Uri $uriProject -Method get -Headers $AzureDevOpsAuthenicationHeader
Foreach ($project in $ProjectsResult.value)
{
do
{
$uriTeams = $UriOrganization + "_apis/projects/$($project.id)/teams?$top=$($top_param)&$skip=$($skip_param)"
$TeamsResult = Invoke-RestMethod -Uri $uriTeams -Method get -Headers $AzureDevOpsAuthenicationHeader
Foreach ($team in $TeamsResult.value)
{
$uriTeamMembers = $UriOrganization + "_apis/projects/$($project.id)/teams/$($team.id)/members?$top=$($top_param)&$skip=$($skip_param)"
$TeamMembersResult = Invoke-RestMethod -Uri $uriTeamMembers -Method get -Headers $AzureDevOpsAuthenicationHeader
Foreach ($teamMember in $TeamMembersResult.value)
{
$Members += New-Object -TypeName PSObject -Property @{
ProjectID=$project.id
ProjectName=$project.name
TeamID=$team.id
TeamName=$team.name
TeamDescription=$team.description
MemberID=$teamMember.identity.id
MemberName=$teamMember.identity.displayName
MemberEmail=$teamMember.identity.uniqueName
}
}
}
$Members | ConvertTo-Csv | Out-File -FilePath "filepath\file.csv" -Append
$top_param=$top_param+1000
$skip_param=$skip_param+1000
}
while (1000 -lt $TeamsResult.count)
}
The issue is that my script is returning me the same results over and over again, pagination is not working. I'm using the following documentation:
Projects Api: https://docs.microsoft.com/en-us/rest/api/azure/devops/core/projects/list?view=azure-devops-rest-6.0