I was provided with a script (How to import json file saved in local directory (desktop) to Azure DevOps Library Variable Group using az cli?) to import a single json file.
However I now need help with a script to import multiple json files to Azure DevOps variable library group.
There are three json file in the path. However the following script only imports one json file.
$jsonFilesDirectory = "C:variables"
$jsonFiles = Get-ChildItem -Path $jsonFilesDirectory -Filter *.json
foreach ($json in $jsonFiles)
{ $jsonnames = $json.Name
$jsonContent = Get-Content -Path "C:variables$jsonnames" | Out-String
$variableGroup = $jsonContent | ConvertFrom-Json
$groupName = $variableGroup.name
$description = $variableGroup.description
$variablesObject = $variableGroup.variables
$variablesHashtable = @{}
foreach ($property in $variablesObject.PSObject.Properties) {
$variableName = $property.Name
$variableValue = $property.Value.value
$variablesHashtable[$variableName] = $variableValue
}
$variablesString = $variablesHashtable.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" } }
# Create variable group using Azure CLI
az pipelines variable-group create --name $groupName --variables $variablesString --description $description --detect false --org 'https://dev.azure.com/<org-Name>' --project '<Project-name>'
2
Answers
You should put
az pipelines variable-group create...
in your foreach loop.remove
}
after$variablesString ...
, put it afteraz pipelines variable-group create...
.script:
To create a multiple variable group with multiple variables in the Azure DevOps Library using multiple JSON files, you may use the PowerShell script below.
Output: