Home > Software design >  Represent a list varible in an Azure DevOps pipeline, for use with PowerShell
Represent a list varible in an Azure DevOps pipeline, for use with PowerShell

Time:10-26

I would like to have a list of items that I can iterate in PowerShell scripts in several stages of a release pipeline.

How can I represent a pipeline variable with multiple values, like a list? For instance, this variable could contain a list of languages.

What would be the syntax to access this pipeline variable from PowerShell?

CodePudding user response:

Here is the syntax to pass a yaml list from an Azure DevOps pipeline parameter to an inline PowerShell script. The key is to use the convertToJson yaml method to convert the yaml object to json. Then converting that json into a PowerShell object with ConvertFrom-Json.

Parameters

    parameters:
    - name: environmentVariableList
      type: object
      default:
        - key: ASPNETCORE_ENVIRONMENT
          value: DV
        - key: Variable2
          value: Value2

Inline Script:

InlineScript: |
  $environmentVariableList = '${{ convertToJson(parameters.environmentVariableList) }}' | ConvertFrom-Json
  foreach($i in $environmentVariableList)
  {
    Write-Host "Setting environment variable [$($i.key)] to [$($i.value)]"
    [Environment]::SetEnvironmentVariable($i.key, $i.value, "Machine")
  }
  • Related