skip to Main Content

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


  1. You should put az pipelines variable-group create... in your foreach loop.

    remove } after $variablesString ..., put it after az pipelines variable-group create....

    script:

    $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>' --project '<project>'
    
    }
    

    enter image description here

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search