简体   繁体   中英

Powershell (?) transforms argument in a very weird way - removes comma from string

I have a powershell build step in TeamCity:

param ([string] $a)

Write-Host "`$a is '$a'."

and in this step I set parameter $a as -a "%TestParam%" or as "-a %TestParam%" , where TestParam has two lines abra and cadabra .

When I run the build I get the following output:

[Step 1/10] PowerShell arguments: -NoProfile, NonInteractive, -ExecutionPolicy, ByPass, -File, C:\buildAgent\temp\buildTmp\powershell1746295357460795314.ps1, -a, "abra, cadabra"
[Step 1/10] $a is 'abra cadabra'.

The only question I have: What on earth happened to the comma? Why has it disappeared?

If I do not use quotes at all ( -a %TestParam% ), then TeamCity passes each line as a separate parameter and I see $a is 'abra'. .

Mathias R. Jessen's answer explains PowerShell's parsing of , -separated tokens as arguments [ his answer has since been deleted, but I hope it will be undeleted ], but that doesn't apply in the case at hand , because any arguments passed to PowerShell's CLI via -File are not subject to PowerShell's command-line parsing - instead, such arguments are treated as literals .

That is, if the command line invoked by TeamCity truly were the following:

powershell ... -File C:\...ps1 -a "abra, cadabra"

then parameter variable $a would receive value abra, cadabra , as expected.

In other words: What is actually being passed in your case must be abra cadabra , not
abra, cadabra , so you need to revise the value of %TestParam% to ensure that it actually contains the desired comma .


As for why the log of the command invoked suggests that there is a , present in what you're passing:

I can only speculate , based on your own guess:

I suspect TeamCity of being a liar, showing lines joined with comma, but passing them without it.

Perhaps TeamCity, when logging invocation of a command line, naively breaks that command line into tokens by whitespace only , without considering quoting , and then presents them as a , -separated list.

If this is indeed the case, then argument "abra cadabra" - without comma - would be logged as
"abra, cadabra" , which would explain the confusion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM