6

From powershell

Set-Alias msbuild C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe

and running from the same directory as my sln file

> msbuild /nologo /property:DefineConstants=FOO

works just fine. Let's say I need two build time constants though.

> msbuild /nologo /property:DefineConstants=FOO;BAR

Powershell interprets the semi-colon as an end-of-statement and runs with a single constant and then tries to run the command BAR and errors. So not what I want.

How about?

> msbuild /nologo /property:DefineConstants="FOO;BAR"
MSBUILD : error MSB1006: Property is not valid.
Switch: BAR

This doesn't work either. Not sure what's going on here but it seems to be interpreting "FOO;BAR" as some sort of single thing - not two constants.

So. How do I actually pass in multiple constants?

Edit Upon further investigation, I'm running the following in cmd.exe

>C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe /nologo /property:DefineConstants=FOO;BAR
MSBUILD : error MSB1006: Property is not valid.
Switch: BAR

although this works

>C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe /nologo /property:DefineConstants="FOO;BAR"

3 Answers 3

8

We can use msbuild escape characters as msbuild /t:rebuild "/property:DefineConstants=PROD%3BCODE_ANALYSIS". Where %3B is to use ; in properties.

https://msdn.microsoft.com/en-us/library/bb383819.aspx

https://msdn.microsoft.com/en-us/library/ms228186.aspx

1
  • 2
    surprisingly, this only worked for me when using xbuild (it fails with msbuild)
    – knocte
    Commented May 20, 2021 at 13:36
7

Two ideas:

Add quotes around the property flag and escape the double quotes

msbuild /nologo "/property:DefineConstants=`"FOO;BAR`""

Use the call operator & (works in PS v4+)

& msbuild /nologo /property:DefineConstants="FOO;BAR"
7
  • Should be noted that while this works for the DefineConstants property it breaks for stuff that takes paths if the path contains a space. So in that case do not use this trick. Commented Jul 18, 2014 at 18:43
  • @GeorgeMauer String escaping for command line tools is one of the areas where powershell can be difficult to use. I noticed that the second option was working for me on version 4.0 but not version 3.0. Commented Jul 18, 2014 at 19:27
  • @GeorgeMauer Can you give an example for the case with a path with a space? Are you talking about setting a property to a path with a space or just a path to a proj/sln file? Commented Jul 18, 2014 at 19:29
  • In my build script I just tried escaping all /property:... as you suggested, and that didn't work since some of them were for things like OutDir. Only escaping the DefineConstants clause worked fine Commented Jul 18, 2014 at 19:31
  • You could try using single quotes on the paths, something like msbuild "/OutDir:'\path\to the\output'". Commented Jul 18, 2014 at 19:34
1

None of the solutions above worked for me so I had to employ another approach: https://stackoverflow.com/a/32326853/544947

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.