azure-pipelines-agent: [YAML] Cannot get PowerShell script to evaluate template expression

I’m trying to get a powershell script inside a reusable step to recognize a parameter in a template expression and no combination of values seems to work.

parameters:
    someVar: 1

steps:
- powershell: |
      $someVar = ${{ parameters.someVar }}

The above results in an error in PowerShell saying variables containing { should be changed to `{.

Have you tried trouble shooting?

Yes

Agent Version and Platform

Version of your agent? 2.102.0/2.100.1/…

OS of the machine running the agent? Windows

VSTS Type and Version

VisualStudio.com or On-Prem TFS? VSTS

If VisualStudio.com, what is your account name? https://fsmb.visualstudio.com

What’s not working?

Cannot get template expressions or parameters to be usable in PowerShell steps.

I have also tried:

$someVar = ${{ format('{0}', parameters.someVar) }}
$someVar = "${{ format('{0}', parameters.someVar) }}"
$someVar = `${{ format("{0}", parameters.someVar) }}`
$someVar = "${{ parameters.someVar }}"
$someVar = '${{ parameters.someVar }}'

In some cases it doesn’t generate an error in PowerShell but it doesn’t evaluate the expression either. I just get the original expression as a string.

Interestingly this properly replaces the template expression.

steps:
- powershell: "${{ format('.\\steps\\do-work.ps1 -major {0}', parameters.someVar) }}"

In this approach I’m calling a separate PowerShell script. However this does not work for my scenario because the file cannot be found. I got this sample from the vsts-tasks repo where they are doing a similar thing. But in my case I’m pulling the (reusable) step from a different repo than the actual yaml build file. So it seems like the pathing isn’t correct such that the external script could be found. Hence I’m switching to a script block inside the reusable step for now.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

@J0F3, I am not really familiar with .yml format, but I was able to give my powershell task a pipeline parameter like this :

- task: PowerShell@2
  inputs:
    targetType: 'filePath'
    filePath: ./Scripts/script.ps1 
    arguments: '-input1 ''$(parameters.param1)'''

Please notice that I use simple quote ! I also suggest you to create your complexe string (with your spaces) elsewhere so that you can simplify your yml script. Don’t know if it’s a good practice though ?

Does it helps ?

Great to hear, this has been the most frustrating part of working with YAML so far.

@CoolDadTx currently the entire value must be an expression. So if you had a multi-line script you could do something like this:

- powershell: |
    ${{ format('
    $someVar1= "{0}"
    $someVar2= "{1}"
    ',
    parameters.someVar1,
    parameters.someVar2) }}

Next sprint we are rolling out support for expressions embedded within a string, to enable this:

- powershell: |
    $someVar1 = ${{ parameters.someVar1 }}
    $someVar2 = ${{ parameters.someVar2 }}

The sprint deployment will begin after next week, and takes roughly 3 weeks to roll across all accounts.

@davidbrownellMS, yes. As Eric has mentioned (in several posts ironically) template expressions only work if they start the string itself. So if you need to mix an expression with anything else then you need to use the format function. So in your case I believe this might work.

- script: ${{ format('echo {0}, {1}, {2}, {3}', parameters.value, parameters.value, parameters.value, parameters.value) }}

If the line doesn’t start with ${{ then the parser doesn’t look for template expressions at all.