Nilay
Nilay

Reputation: 279

Does Azure DevOps remove quotation marks when setting a pipeline variable via a script?

I have to set an array for an ARM Template within my Azure DevOps pipeline. I set my variable like this:


$foo = '["' + 'bar' + '"]'
Write-Host $foo
#######Assign Variables with Correct Value in pipeline
Write-Host "##vso[task.setvariable variable=fooVariableInPipeline]$foo"

The $foo value is fine which would be ["bar"] as expected. But when I inspect the value in the next tasks after setting the variable, it would remove the quotation marks and show the value as [bar].

This is rather strange and I am wondering if its me or is it a bug? Any workarounds?

For the time being, I have updated my file directly using powershell code.

Upvotes: 9

Views: 7584

Answers (3)

Joseph Luce
Joseph Luce

Reputation: 163

I ran into this issue myself.

A workaround is to use two double quotes.

In powershell:

$foo -replace '"', '""'

Upvotes: 0

Leo Liu
Leo Liu

Reputation: 76918

Does Azure DevOps remove quotation marks when setting a pipeline variable via a script?

This issue does not come from azure devops, but depends on the syntax of your next task.

If I use the command line task to output that variable, it works fine:

echo $(fooVariableInPipeline)

The output:

enter image description here

However, if we use the powershell task to output it, it will remove quotation marks:

Write-Host "fooVariableInPipeline is $(fooVariableInPipeline)"

The output:

enter image description here

To resolve this issue, we need to surround the variable with single quotes to make it a literal string, like:

$Testfoo= '$(fooVariableInPipeline)'

Write-Host "Testfoo is $Testfoo"

Now, it works fine:

enter image description here

Hope this helps.

Upvotes: 5

derekbaker783
derekbaker783

Reputation: 9611

You could try a here-string:

$foo = @"
    ["bar"]
"@

Upvotes: 1

Related Questions