Skip to main content

Using Azure DevOps REST API with PowerShell - Part 3

· 4 min read
Hasan Gural

In the last article of our series, we will try to access the endpoint of Pipelines, and we will list pipelines expressions of all projects.

First of all, we need to look at the list of endpoint types down below.

Operation types for Pipelines

Operation NameOperation Detail
CreateCreate a pipeline.
GetGets a pipeline, optionally at the specified version
ListGet a list of pipelines

Get all pipelines for specific AzDo Project

Let's go right to the example side and execute our first query on API. What we want to do is relatively easy since it is only receiving the pipeline list of a particular Azure DevOps project. We will use the List API endpoint for that purpose.

$OrganizationName = "hasangural" # Azure DevOps OrganizationName
$ProjectName = "GBC-TerraformDemo"
$AzDoToken = "PAT-TOKEN-HERE" # Azure DevOps Personal Access Token (PAT)

# Authentcation Header for AzDo REST Endpoint.
$AuthenicationHeader = @{Authorization = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$AzDoToken")) }

# Generating environment variables

Set-Variable -Name "azDoAuthHeader" -Value $AuthenicationHeader -Scope Global -ErrorAction Stop
Set-Variable -Name "azDoBaseUrl" -Value "https://dev.azure.com/$OrganizationName" -Scope Global -ErrorAction Stop

$uri = "$azDoBaseUrl/$($ProjectName)/_apis/pipelines?api-version=7.1-preview.1"

# Rest API params
$restParams = @{

"URI" = $uri
"Headers" = $azDoAuthHeader
"Method" = "GET"
"ErrorAction" = "Stop"

}

$getPipelines = Invoke-RestMethod @restParams

$getPipelines.value | ForEach-Object {

Write-Output '````````````````````````````````````````````````````````````'
Write-Output "[INFO] Pipeline Name: $($_.Name)"
Write-Output "[INFO] Pipeline URL: $($_.url)"
Write-Output '````````````````````````````````````````````````````````````'

}

If you read the PowerShell lines above, there are two vital criteria. These are in order:

  • OrganizationName
  • ProjectName

We can execute this query on any project in any organization thanks to these two variables. Our result is displayed below.

Let's expand our scenario by targeting the complete list of pipelines. What we want to do is to get all the pipelines in an organization. Of course, we need to have 'reader' permission in all projects at least to complete this mission.

The things we need to do in PowerShell in order: -Get all the projects from the Projects API endpoint -After that, visit every project and find all the pipeline expressions.

$OrganizationName = "hasangural" # Azure DevOps OrganizationName
$AzDoToken = "PAT-TOKEN-HERE" # Azure DevOps Personal Access Token (PAT)

# Authentcation Header for AzDo REST Endpoint.
$AuthenicationHeader = @{Authorization = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$AzDoToken")) }

# Generating environment variables

Set-Variable -Name "azDoAuthHeader" -Value $AuthenicationHeader -Scope Global -ErrorAction Stop
Set-Variable -Name "azDoBaseUrl" -Value "https://dev.azure.com/$OrganizationName" -Scope Global -ErrorAction Stop

$uri = "$azDoBaseUrl/_apis/projects?api-version=6.1-preview.1"

# Rest API params
$restParams = @{

"URI" = $uri
"Headers" = $azDoAuthHeader
"Method" = "GET"
"ErrorAction" = "Stop"

}

$getProjects = Invoke-RestMethod @restParams

$getProjectNames = $getProjects.value.name


ForEach ($project in $getProjectNames) {

$ProjectId = $project.id

$getPipelines = "$azDoBaseUrl/$($project)/_apis/pipelines?api-version=6.1-preview.1"

$getOutputPipelines = Invoke-RestMethod -Uri $getPipelines -Method Get -Headers $azDoAuthHeader

$getOutputPipelines.value | ForEach-Object {

Write-Output '````````````````````````````````````````````````````````````'
Write-Output "[INFO] ProjectName: $($project)"
Write-Output "[INFO] PipelineName: $($_.Name)"
Write-Output "[INFO] PipelineId: $($_.Id)"
Write-Output '````````````````````````````````````````````````````````````'

}
}
Request Order for AzDo REST Api

The most important part of the PowerShell code block above is using the Projects endpoint to access the complete list of projects first. Then, it gets the entire collection of projects and uses the Pipelines endpoint to find a result by making queries one for each. If there is no noticeable problem in this logic, it is possible to have a result like down below.

As you can see, we managed to execute many REST API requests. Thus we tried to lean on more different sides of automation. If you are invested in Azure DevOps REST API with PowerShell, you can give further examples for the sake of continuity of our series.