Search code examples
powershellazure-devopsazure-cliazure-pipelines-yaml

Retrieve Variable Name and Value from az pipeline variable-group


I would like to loop through items in a variable-group where the name starts with add_ and then add these to keyvault.

I have got the variables from a variable-group with:

az pipelines variable-group variable list with additional parameters to return name values:

{
  "add_myVaule": {
    "isSecret": null,
    "value": "hello"
  },
  "add_myWorld": {
    "isSecret": null,
    "value": "world"
  }
}

I'm not sure how I can reference the name (add_myVaule, add_myWorld) and get the value so i can loop through and add the values into a keyvault, something like this:

foreach ($var in $vargroup){
   az keyvault secret set --vault-name $(VaultName) --name $var --value $var.value
}

Solution

  • You can retrieve variable name and value by the following steps: 1 az pipelines variable-group variable list is a JSON string from your screenshot, you should convert a JSON string to a hash table

    ' your JSON String ' | ConvertFrom-Json -AsHashtable
    

    And you can see the Hash Table Data structure

    JSON to Hash

    2 run the script and the $($.key) is to get the Key(add_myVaule,add_myWorld), $($.value) is to get the corresponding value

    $variable_group.GetEnumerator() | % { 
        "Current key is: $($_.key)"
            $($_.value).GetEnumerator() | %{
            "Current key is: $($_.key)"
            "Value of $($_.key) is: $($_.value)"
         }
    }
     
    

    print variable name and value