[Code Snippet] PowerShell | Azure DevOps | Query Variable Group using API
In Azure DevOps, Variable groups store values and secrets that you might want to be passed into a YAML pipeline or make available across multiple pipelines.
Variable Group is a collection of variables with Name and Value pair.
Below is my variable Group ‘BillingSystemDictionary’ with 3 variables in my DevOps organization ‘RajeevPentyala0011’ and project ‘Demo’ (These details are required in PowerShell script).
In DevOps pipeline scenarios, you may need to fetch the variables under a variable group. Following is the PowerShell script which calls DevOps API and fetch variables by variable group name.
# Form API authentication Header
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $(System.AccessToken)")
$headers.Add("Content-Type", "application/json")
# Pass Variable Group Name and read variables
$variableGroupName = "BillingSystemDictionary"
# 'projectid' pattern is https://dev.azure.com/{organization_name}/{projectname}
$projectid = "https://dev.azure.com/RajeevPentyala0011/Demo"
$variableGroupGetUrl = "$projectid/_apis/distributedtask/variablegroups?groupName=$variableGroupName*&queryOrder=IdDescending&api-version=6.0-preview.2"
$queryResponseObject = Invoke-RestMethod -Uri $variableGroupGetUrl -Method GET -Headers $headers
# $queryResponseObject will be of type pscustomobject
# To fetch the variable value of "isactive"
$variableName = "isactive"
$variableValue = $queryResponseObject.value.variables.$variableName.value
Write-Host "$variableName value is $variableValue"
🙂
Categories: CRM, DevOps
API, Azure Devops, PowerShell, Variables
Comments (0)
Trackbacks (0)
Leave a comment
Trackback