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
  • 85,268
  • 51
  • 226
  • 362
  • 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

22

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
  • 184,219
  • 38
  • 329
  • 358
  • 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
4

Also using ascii value might help:

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

  • 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
-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,336
  • 23
  • 22