How to: List all Environments and Approvers on Azure DevOps

Vinicius Moura
2 min readJul 24, 2023

--

This script and report extract all Environments and respective Approvers to Multi-Stage Pipeline YAML on each Team Project within Azure DevOps

An original script is available on my GitHub repository. See below this script:

Let’s go understand each command used.

  1. The PowerShell script will receive the following parameters:
  • $PAT = Personal Access token to connect on Azure DevOps;
  • $Organization = Organization URL to list all Environments and Approvers on Azure DevOps organization;
  • $Connstr = connection string to Azure SQL Database that stores the report information. To create this report, it’s necessary to create previously a previous Azure SQL Server and Database and run the script below:

2. Projects List = Use this REST API to list all projects on the Azure DevOps organization.

$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
$UriOrganization = "https://dev.azure.com/$($Organization)/"
$uriProject = $UriOrganization + "_apis/projects?`$top=500"
$ProjectsResult = Invoke-RestMethod -Uri $uriProject -Method get -Headers $AzureDevOpsAuthenicationHeader
Foreach ($project in $ProjectsResult.value)
{
Write-Host $project.name
}

3. Environments List = use this REST API to list all Environments on each Team Project

$uriEnvironments = $UriOrganization + "$($project.id)/_apis/distributedtask/environments?api-version=6.1-preview.1"    
$EnvironmentsResult = Invoke-RestMethod -Uri $uriEnvironments -Method get -Headers $AzureDevOpsAuthenicationHeader
Foreach ($environment in $EnvironmentsResult.value)
{
Write-Host $environment.name
}

4. Check Configurations Query = use this REST API to list all Approvers and Checks on each Environment

$body = @(
@{
type="queue"
id="1"
name="Default"
},
@{
type="environment"
id="$($environment.id)"
name="$($environment.name)"
}
) | ConvertTo-Json
$uriEnvironmentChecks = $UriOrganization + "$($project.id)/_apis/pipelines/checks/queryconfigurations?`$expand=settings&api-version=6.1-preview.1"
$EnvironmentChecksResult = Invoke-RestMethod -Uri $uriEnvironmentChecks -Method Post -Body $body -Headers $AzureDevOpsAuthenicationHeader -ContentType application/json
Foreach ($envcheck in $EnvironmentChecksResult.value)
{
Write-Host $envcheck.type.name
}

5. If the environment check type name is equal to Approval, getting a list of approvers is necessary. To do this, run an Invoke Method on the URL property and list all approvers on this environment.

$ApproversResult = Invoke-RestMethod -Uri $envcheck.url -Method get -Headers $AzureDevOpsAuthenicationHeader
Foreach ($approver in $ApproversResult.settings.approvers)
{
Write-Host $approver.uniqueName
}

6. After extract all Environments and Approvers, this information is stored in a table in Azure SQL.

7. After insert information into a table, I connected this database to Power BI:

  • Team Projects (1) = Filter report using Team Projects field;
  • Environments (2) = Filter report using Environments field;
  • Approvers (3) = Filter report using Approvers field;
  • Report Information (4) = List all information about Team Projects, Environments, and Approvers.

--

--

Vinicius Moura
Vinicius Moura

Written by Vinicius Moura

Tech Solution Architect Manager na Accenture

No responses yet