Pass argument with quotes to PowerShell script via TeamCity

三世轮回 提交于 2020-01-23 08:02:29

问题


We're calling a PowerShell script via TeamCity. We want to pass a parameter which contains quotes, e.g.:

Build step       : PowerShell
Script file      : foo/bar/my.ps1
Script arguments : -MyParam "%system.MyParam%"

Where system.MyParam is set to <xml><elem attr="value"></elem></xml>. Unfortunately, this fails with:

Cannot process argument transformation on parameter MyParam. Cannot convert value "<xml><elem" to type "System.Xml.XmlDocument". Error: "Unexpected end of file while parsing Name has occurred. Line 1, ..."

Anybody knows how to correctly pass double quotes?

What I've tried so far and didn't work:

  • Simply passing the parameter
  • Using double double quotes (i.e. attr=""value"")
  • Using PowerShell's backtick escape mechanism (i.e. attr=`"value`")
  • Using single quotes around the whole parameter (works only if value does not contain spaces)

回答1:


As your problem is that the input XML string contains double-quoted attribute values, a possible workaround could be using single-quoted attribute values.




回答2:


Backtick (`) is the PSH escape character. So you can use "`"" to pass a string containing a double quote character.

You can also use single quotes around PSH strings – and also avoid expression interpolation: '"' is also a string with a single double quotes.

But remember you need to ensure the quoting works for both the launcher (sending the arguments) and for the script itself: you may need to escape the escapes as well.



来源:https://stackoverflow.com/questions/32246144/pass-argument-with-quotes-to-powershell-script-via-teamcity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!