18

We have a batch file that invokes our MSBuild-based build process. Syntax:

build App Target [ Additional MSBuild Arguments ]

Internally, it does this:

msbuild.exe %1.msbuild /t:%2 %3 %4 %5 %6 %7 %8 %9

Which results in calls to MSBuild that look like this:

msbuild.exe App.msbuild /t:Target

When any argument contains the equal sign, =, Powershell completely removes it. My batch script never sees it. This does not happen with the standard cmd.exe command prompt.

For example, if I call

build App Target "/p:Property=Value"

this is what gets passed to MSBuild:

msbuild.exe App.msmbuild /t:Target /p:Property Value

I expected this:

msbuild.exe App.msbuild /t:Target "/p:Property=Value"

I've tried the Powershell escape character, the standard Command Prompt escape character, and even stuff I made up:

build App Target "/p:Property=Value"
build App Target '/p:Property=Value'
build App Target /p:Property^=Value
build App Target /p:Property`=Value
build App Target /p:Property==Value

None of it works. What do I do to get the equal sign to not be stripped out or removed?

7 Answers 7

25

I've seen this before and have found a way to trick it out. I wish I could explain what's going on in particular with the '=' but I cannot. In your situation I'm fairly certain the following will work if you want to pass properties to msbuild:

build App Target '"/p:Property=Value"' 

When echoed, this produces the following:

msbuild.exe App.msbuild /t:Target "/p:Property=Value"
Sign up to request clarification or add additional context in comments.

6 Comments

That worked! I thought I tried that combination, but I guess I just tried only single quotes, not single quotes around double quotes. Powershell can be quite strange sometimes.
The problem is that PowerShell does not quote the arguments when passed to executables. So even though PowerShell knows that "/p:Property=Value" is one string, it passes it without quotes. This can cause executables to not see it as one argument. I had the same problem with include quotes in an argument, and I had to \ escape them.
@jasonmarcher: Agreed, but this seemed to be unique with the '=' as escaping it made no difference. Give it a try and you'll see.
The quoting is the issue, I'm completely agreeing with you.
After re-reading your comment, I see what you mean. Sorry, my misunderstanding. :)
|
5

With PowerShell 3 you can use --% to stop the normal parsing powershell does.

build --% App Target "/p:Property=Value"

Comments

0

I don't know if there's an easier answer (I think not) but you can solve the problem by using .Net's process class to invoke cmd.exe. Here's an example:

# use .NET Process class to run a batch file, passing it an argument that contains an equals sign. 
# This test script assumes the existence of a batch file "c:\temp\test.bat"
# that has this content:
#      echo %1
#      pause
$cmdLine =  $cmdLine =  '/c c:\temp\test.bat "x=1"'
$procStartInfo =  new-object System.Diagnostics.ProcessStartInfo("cmd", $cmdLine )
$proc = new-object System.Diagnostics.Process
$proc.StartInfo = $procStartInfo
$proc.Start();

2 Comments

This won't work. Invoking the batch file has to be a one line operation, as outlined in my question.
Well, you could make it a function, then it could be a one-liner. But I hope scott's answer wroks.
0

Have you tried single quotes to force a literal interpretation?

Or: cmd /c 'msbuild.exe App.msbuild /t:Target "/p:Property=Value"'

1 Comment

Invoking a batch file will drop the = sign too, though in this case enclosing in quotes is sufficient to preserve it.
0

It seems that only single-quote around double-quote might be the best for multiple scenario around windows environment. Following link from MS shows its support(or limitation) of equal sign http://support.microsoft.com/kb/35938 It is specific to Batch Files but it likely affect lots of other MS shell products.

Comments

0

The answer is that %2 becomes "/p:property" and %3 becomes "value".

Make this work in your batch file by using BOTH %2 and %3 and insert an = sign between them:

msbuild.exe %1.msbuild /t:%2=%3 %4 %5 %6 %7 %8 %9

and do not use the quote chars on the command line call. Use:

build App Target /p:property=value

For additional args with = signs just keep pairing them up.

I had the same issue with a simple batch file to run youtube-dl where the URL I pass has an = sign in it. solved as :

@echo off
REM YTDL audio only
echo %1=%2
youtube-dl -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 %1=%2

Comments

0

I put the equal sign in a pair of double quotes, and when passed to the command file, which runs the FINDSTR command, the command completely ignores the double quotes, and treats the equal sign as a normal parameter.

E.G. the command line 'runfindstr.cmd if @string "=" *.txt, returns all *.txt files with text "if @string =" in any of the lines.

If the command you are using doesn't ignore the double quotes, you can always put multiple versions of the command in the command file, one of which is preceded with 'if %n equ "="' (where n is the relative position of the parameter) then carry out command with a hard coded = character.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.