How do I pass a property value containing a semico

2020-06-09 06:01发布

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?

3条回答
男人必须洒脱
2楼-- · 2020-06-09 06:29

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
查看更多
一夜七次
3楼-- · 2020-06-09 06:31

Also using ascii value might help:

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

查看更多
放我归山
4楼-- · 2020-06-09 06:40

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"
查看更多
登录 后发表回答