azure-devopsazure-pipelinesazure-pipelines-yamlazure-yaml-pipelines

Pass an array of string to a powershell script from a yaml template


I have the following step in a yaml file which uses a template:

steps:
  - template: 'templatePool.yml'
    parameters:
      Action: "Stop"
      MachineDeploiement: 'system1'
      ScriptsRemoteFolder: 'c:\dev\scripts'
      PoolNames:
        - 'pool1'
        - 'pool2'

The templatePool yaml file:

parameters:
  Action: ''
  MachineDeploiement: ''
  ScriptsRemoteFolder: ''
  PoolNames: []

steps:
  - task: PowerShellOnTargetMachines@3
    displayName: 'exec script'
    inputs:
      Machines: '${{parameters.MachineDeploiement}}'
      UserName: '$(User)'
      UserPassword: '$(Pwd)'
      ScriptType: FilePath
      ScriptPath: '${{parameters.ScriptsRemoteFolder}}\AppPool.ps1'
      ScriptArguments: '-AppPoolNames ${{parameters.PoolNames}} -Action ${{parameters.Action}}'

the AppPool.ps1 accept an array of string as a parameter: [string[]] $PoolNames
Usually, outside of yaml, I call it using @('pool1','pool2')
When trying to execute the pipeline, I get the message: unable to convert Array to String. Value : Array
How to get through this?

It seems that the array type is not well managed in the yaml... I expect that the pipeline could be executed without any error


Solution

  • you can reference the samples below to configure your pipeline:

    1. The sample of PowerShell script.

      # AppPool.ps1
      
      param (
          [string[]] $PoolNames
      )
      
      foreach ($PoolName in $PoolNames)
      {
          Write-Host "--------------------"
          Write-Host "$PoolName"
      }
      
    2. In the template file "templatePool.yml", define the parameter 'PoolNames' as string type and set the default value as empty.

      # templatePool.yml
      
      parameters:
      . . .
      
      - name: PoolNames
        type: string
        default: ''
      
      steps:
      - task: PowerShell@2
        displayName: 'Run PowerShell script'
        inputs:
          filePath: 'path/to/AppPool.ps1'
          arguments: '-PoolNames ${{ parameters.PoolNames }}'
          pwsh: true
      
    3. In the pipeline main YAML, when calling the template, use comma as separation to pass a list of substrings as the value of the parameter 'PoolNames'.

      # azure-pipelines.yml
      
      steps:
      - template: templatePool.yml
        parameters:
          PoolNames: >
            pool1,
            pool2,
            'my pool3'
      
    4. The result.

      enter image description here

    NOTE:

    If a pool name contains whitespace, you should use the double/single quotes to surround it when entering the parameter value (see 'my pool3' in above example). Otherwise, the value will be truncated by the whitespace when passing into the PowerShell script (see the example below).

    # azure-pipelines.yml
    
    steps:
    - template: templatePool.yml
      parameters:
        PoolNames: >
          pool1,
          pool2,
          my pool3
    

    enter image description here

    There is a similar case as reference: