Skip to content

Instantly share code, notes, and snippets.

@stefanstranger
Last active January 29, 2021 13:30
Show Gist options
  • Save stefanstranger/f510941b5991a778f4b45e11bbadc0f7 to your computer and use it in GitHub Desktop.
Save stefanstranger/f510941b5991a778f4b45e11bbadc0f7 to your computer and use it in GitHub Desktop.
PowerShell script to retrieve Azure DevOps Yaml Pipelines
<#
PowerShell script to retrieve Azure DevOps Yaml Pipelines
Requires:
- PSDevOps PowerShell Module (Install-Module PSDevOps)
- powershell-yaml PowerShell Module (Install-Module powershell-yaml)
- Personal Access Token for Azure DevOps Organization and Project where you want to retrieve the Yaml Pipelines
Example: Return all Tasks within a job and steps.
$YamlPipelines | Select-Object -ExpandProperty Yaml |
Select-Object -ExpandProperty jobs |
Select-Object -ExpandProperty steps |
Select-Object @{'L'='Task';E={$_.task}}, @{'L'='DisplayName';E={$_.displayName}}
Example: Use RawYaml and RegEx to search in Yaml Pipelines.
Foreach ($Yaml in $YamlPipelines) {[regex]::Match(($Yaml | Select-Object -ExpandProperty RawYaml),"/*displayName:\s*([^\n\r]*)" ) | Select Value}
#>
$Organization = '[Enter Name of Azure DevOps Organization]'
$Project = '[Enter Name of Project]'
$Null = Connect-ADO -Organization $Organization -PersonalAccessToken '[Enter Persons Access Token]'
$BuildDefinitions = Invoke-ADORestAPI -Uri ('https://dev.azure.com/{0}/{1}/_apis/build/definitions' -f $Organization, $Project)
$YamlPipelines = @()
Foreach ($YamlBuild in $BuildDefinitions) {
$BuildDefinition = Get-ADOBuild -Organization $Organization -Project $Project -DefinitionID $($YamlBuild.id) -DefinitionYAML -ErrorAction SilentlyContinue
if (![System.String]::IsNullOrEmpty($BuildDefinition)) {
$YamlPipelines += [PSCustomObject]@{
Name = $($Yamlbuild.name)
Id = $($Yamlbuild.id)
Yaml = $BuildDefinition | ConvertFrom-Yaml
RawYaml = $BuildDefinition
}
}
}
$YamlPipelines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment