Skip to content

Instantly share code, notes, and snippets.

@flcdrg
Last active September 16, 2022 06:44
Show Gist options
  • Save flcdrg/767a49b56da5c08b569bb45aa76c7af2 to your computer and use it in GitHub Desktop.
Save flcdrg/767a49b56da5c08b569bb45aa76c7af2 to your computer and use it in GitHub Desktop.
PowerShell script to convert an Azure DevOps Pipeline variable group to the equivalent YAML
<#
.SYNOPSIS
Convert Azure Pipelines Variable Group to YAML
.DESCRIPTION
Reads a variable group from an Azure DevOps pipeline library and converts it into the equivalent YAML
.NOTES
Requires the Azure CLI, with the azure-devops extension installed. eg. `az extension add --name azure-devops`
.PARAMETER GroupName
The naem of the variable group
.PARAMETER Organisation
The Azure DevOps organisation name
.PARAMETER Project
The name of the Azure DevOps project
.PARAMETER Array
Output using the YAML 'Array' syntax (-name: value:)
.EXAMPLE
.\Convert-VariableGroup.ps1 -Organisation gardiner -GroupName "My Variable Group" -Project "GitHub Builds"
Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
#>
param (
[Parameter(Mandatory=$true)]
[string]
$GroupName,
[string]
$Organisation,
[string]
$Project,
[switch]
$Array
)
$ErrorActionPreference = 'Stop'
# Find id of group
$groups = (az pipelines variable-group list --organization "https://dev.azure.com/$Organisation" --project "$Project") | ConvertFrom-Json
$groupId = $groups | Where-Object { $_.name -eq $GroupName } | Select-Object -ExpandProperty id -First 1
$group = (az pipelines variable-group show --id $groupId --organization "https://dev.azure.com/$Organisation" --project "$Project") | ConvertFrom-Json
$group.variables | Get-Member -MemberType NoteProperty | ForEach-Object {
if ($Array.IsPresent) {
Write-Output "- name: $($_.Name)"
Write-Output " value: $($group.variables.$($_.Name).Value)"
} else {
Write-Output " $($_.Name): $($group.variables.$($_.Name).Value)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment