Powershell VSTS任务的转义参数

时间:2018-10-03 18:46:28

标签: azure-devops

相关(但不能回答这种情况):VSTS Task Group Powershell Parameter

问题:如何传递带有双引号和其他可能不舒服的值的参数(例如单引号和双引号以及其他特殊字符的组合)

我不知道可能是什么参数。因此,需要一种逃避仲裁输入的保证方法。

例如,当传递这样的参数时

-ParamName "$(ParamValue)"

和这样的值:

[ "abc=xyz", "abc=somethingelse" ]

我得到了以下内容:

2018-10-03T17:51:53.3159259Z Generating script.
2018-10-03T17:51:53.3257527Z Formatted command: . 'M:\<...>\blah.ps1' -ParamName "[ "abc=xyz", "abc=somethingelse" ]" <...>
2018-10-03T17:51:53.3969661Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'M:\...\_temp\14d05e56-d7c7-4db9-8007-a29f5b024b51.ps1'"
2018-10-03T17:51:53.7924531Z M:\<...>\blah.ps1 : A 
2018-10-03T17:51:53.7924970Z positional parameter cannot be found that accepts argument 'abc=xyz, abc=somethingelse ]'.

1 个答案:

答案 0 :(得分:0)

有2种解决当前问题的方法,第一种是在传递参数时简单地使用单引号,例如:

-ParamName '$(ParamValue)'

另一种方法是使用“`”字符(称为反引号,反引号或重音符)对参数值中的双引号进行转义。因此,值从以下更改:

[ "abc=xyz", "abc=somethingelse" ]

收件人:

[ `"abc=xyz`", `"abc=somethingelse`" ]

这样,参数值中的第一个双引号将不会被解释为字符串的结尾。