Search code examples
azureazure-devopsazure-cli

How to import multiple json file that are saved in local directory (desktop) to Azure DevOps Library Variable Group using az cli?


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>'

Solution

  • How to import multiple json file that are saved in local directory (desktop) to Azure DevOps Library Variable Group using az cli?

    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.

     $jsonFilesDirectory = "C:\variables" 
        
     $jsonFiles = Get-ChildItem -Path $jsonFilesDirectory -Filter *.json
        
        foreach ($json in $jsonFiles) {
            $jsonContent = Get-Content -Path $json.FullName | 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/Mindtreeuser' --project 'Venkat-Demo-project'
        }
    

    Output:

    enter image description here

    enter image description here

    enter image description here