简体   繁体   中英

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

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?

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"

Also using ascii value might help:

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

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

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