How do I pass an equal sign when calling a batch s

2020-02-01 01:04发布

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?

5条回答
啃猪蹄的小仙女
2楼-- · 2020-02-01 01:42

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"
查看更多
手持菜刀,她持情操
3楼-- · 2020-02-01 01:47

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

build --% App Target "/p:Property=Value"
查看更多
家丑人穷心不美
4楼-- · 2020-02-01 01:48

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();
查看更多
Animai°情兽
5楼-- · 2020-02-01 01:59

Have you tried single quotes to force a literal interpretation?

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

查看更多
相关推荐>>
6楼-- · 2020-02-01 02:00

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.

查看更多
登录 后发表回答