How do I pass a property value containing a semicolon on the MSBuild command line when running it from PowerShell?

泪湿孤枕 提交于 2019-12-21 03:45:12

问题


I'm attempting to pass a property to MSBuild. The property is a semicolon-delimited list of values. Unlike this question, I'm running MSBuild from PowerShell.

I get:

PS> msbuild .\Foo.sln /p:PackageSources="\\server\NuGet;E:\NuGet"

MSBUILD : error MSB1006: Property is not valid.
Switch: E:\NuGet

If I run the same command from Command Prompt, it works fine. How do I get it to work in PowerShell?


回答1:


Wrap the parameter in single quotes:

... '/p:PackageSources="\\Server\NuGet;E:\NuGet"'

On PowerShell v3 try this:

msbuild .\Foo.sln --% /p:PackageSources="\\Server\NuGet;E:\NuGet"



回答2:


Also using ascii value might help:

msbuild .\Foo.sln /p:PackageSources="\Server\NuGet%3BE:\NuGet"




回答3:


VBScript function below can be used to escape property values passed to MSBuild.exe inside double quotes:

    Function Escape(s)
      Escape = s

      Set objRegEx = CreateObject("VBScript.RegExp") 

      objRegEx.Global = True 
      objRegEx.Pattern = "(\\+)?"""

      Escape = objRegEx.Replace(Escape,"$1$1\""") 

      objRegEx.Pattern = "(\\+)$"

      Escape = objRegEx.Replace(Escape,"$1$1") 
    End Function

The following example demonstrates usage of Escape() function

    Set objShell = WScript.CreateObject("WScript.Shell")        
    objShell.Run "msbuild.exe echo.targets /p:Param1=""" & Escape("ParamValue1") & """,Param2=""" & Escape("ParamValue1") & """", 1, True


来源:https://stackoverflow.com/questions/11987384/how-do-i-pass-a-property-value-containing-a-semicolon-on-the-msbuild-command-lin

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