Forum Discussion
rabbani465
Nov 08, 2023Copper Contributor
Regarding automation of Variable Groups using Powershell
Hi, I am a Junior DevOps Engineer. I have a doubt regarding how to create a automated PowerShell script that will reduce the manual time and deliver the work faster. So, my primary thing is I have a...
Kidd_Ip
Mar 18, 2025MVP
Take this:
Using PowerShell
1. Install Azure DevOps CLI:
- Make sure you have the Azure DevOps CLI extension installed:
az extension add --name azure-devops
2. Authenticate:
- Log in to Azure DevOps using a service principal or personal access token (PAT):
az login --service-principal --username "<SPN_APP_ID>" --password "<SPN_SECRET>" --tenant "<TENANT_ID>"
3. Create a Variable Group:
- Use the following command to create a variable group and add variables:
az pipelines variable-group create --name "<GroupName>" --authorize true --variables key1=value1 key2=value2
Using REST API
1. Generate a PAT:
- Create a personal access token (PAT) in Azure DevOps with the necessary permissions.
2. Make API Calls:
- Use the following API endpoint to create a variable group:
POST https://dev.azure.com/{organization}/{project}/_apis/distributedtask/variablegroups?api-version=6.0-preview.2
- Include the variable group details in the request body:
{
"name": "MyVariableGroup",
"variables": {
"key1": {
"value": "value1",
"isSecret": false
},
"key2": {
"value": "value2",
"isSecret": false
}
}
}
3. Automate with PowerShell:
- Use PowerShell to send the API request:
$url = "https://dev.azure.com/{organization}/{project}/_apis/distributedtask/variablegroups?api-version=6.0-preview.2"
$body = @{
name = "MyVariableGroup"
variables = @{
key1 = @{ value = "value1"; isSecret = $false }
key2 = @{ value = "value2"; isSecret = $false }
}
} | ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri $url -Method Post -Headers @{Authorization = "Bearer <PAT>"} -Body $body -ContentType "application/json"