15

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?

Community
  • 1
  • 1
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
  • Try using the backtick escape character ("`") in front of the semicolon. Possibly other characters in that string, as well. – David Aug 16 '12 at 16:58
  • 2
    Backtick doesn't work -- the problem is that MSBuild expects /p:PropA=ValA;PropB=ValB. The semicolon needs 'escaping' from MSBuild, not from PowerShell. Adding the quotes should fix this, but PoSh pulls them out when passing the command line on. – Roger Lipscombe Aug 17 '12 at 08:27

3 Answers3

23

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"
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Can't check v3 (I uninstalled it when it broke my PowerShell console settings -- font, colours, size), but the extra single quotes work. – Roger Lipscombe Aug 17 '12 at 08:28
6

Also using ASCIII value helps:

msbuild .\Foo.sln /p:PackageSources="\\Server\NuGet%3BE:\NuGet"
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Hi, you are answering a pretty old question. Can you elaborate why your answer is better than the ones that were already provided? – Noel Widmer May 30 '17 at 11:03
  • 3
    Sure. In this case, you do not need to change the script, that's only difference. That was actually my problem - I was using distribution plugin that triggered the build. And since I was not allowed to touch the plugin's code, this solved it more suitably. –  May 31 '17 at 09:05
  • Cool! Next time I would add that text directly to your post (edit) where it can be easly found for a future reader. You can still do that (I'd recommend it). – Noel Widmer May 31 '17 at 09:08
  • In case anyone got here the same way I did... I had the same error for dotnet build -p:TreatWarningsAsErrors=true -p:NoWarn="CS0618;CS1705" however %3B (semicolon) did not work but %2C (comma) did. – neoscribe Jun 01 '23 at 22:26
-2

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
Raman Zhylich
  • 3,537
  • 25
  • 23