Powershell调用带嵌套引号的msbuild [英] Powershell call msbuild with nested quotation marks

查看:148
本文介绍了Powershell调用带嵌套引号的msbuild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Powershell和Psake为Visual Studio解决方案创建程序包和部署. 尝试使用msbuild部署数据库项目-使用msdos visual studio命令行可以正常工作

Using Powershell and Psake to create package and deployment for a visual studio solution. Trying to deploy a database project using msbuild - which is working correctly using msdos visual studio command line

   msbuild /target:Deploy /p:UseSandboxSettings=false /p:TargetConnectionString="aConnectionWithSpacesAndSemiColons" "aDatabaseProjectPathWithSpaces"

从powershell调用时,同一方法调用会导致错误

the same method call results in an error when called from powershell

& msbuild /target:Deploy /p:UseSandboxSettings=false /p:TargetConnectionString="aConnectionWithSpacesAndSemiColons" "aDatabaseProjectPathWithSpaces"

与空格有关-无法弄清楚如何在Powershell中复制此调用-示例数据库连接字符串 数据源=.\ SQL2008;初始目录= DocumentExecution;集成安全性= True;

relating to spaces - can't figure out how to replicate this call in powershell - sample database connectionstring Data Source=.\SQL2008;Initial Catalog=DocumentExecution;Integrated Security=True;

推荐答案

简短版

如何在PowerShell中将包含引号的参数传递给本机命令?

  • 在参数字符串中使用单引号而不是双引号:
       "/p:Target='Data Source=(local)\SQL;Integrated Security=True'"
    /p:Target='Data Source=(local)\SQL;Integrated Security=True'

  • Use single quotes instead of double quotes in the argument string:
       "/p:Target='Data Source=(local)\SQL;Integrated Security=True'"
    /p:Target='Data Source=(local)\SQL;Integrated Security=True'

对参数字符串中的双引号使用反斜杠转义 :
   '/p:Target=\"Data Source=(local)\SQL;Integrated Security=True\"'
/p:Target="Data Source=(local)\SQL;Integrated Security=True"

Use backslash-escaping for double quotes in the argument string:
   '/p:Target=\"Data Source=(local)\SQL;Integrated Security=True\"'
/p:Target="Data Source=(local)\SQL;Integrated Security=True"

如果嵌入式引号仅用于将参数视为单个字符串,而不是参数的必需部分,则可以使用以下内容:

If the embedded quotes are only being used to treat the argument as a single string, rather than being a required part of the parameter, then the following can be used:

  • 引用整个参数字符串,而不是在参数中嵌入引号:
       '/p:Target=Data Source=(local)\SQL;Integrated Security=True'
    /p:Target=Data Source=(local)\SQL;Integrated Security=True

  • Quote the entire argument string, instead of embedding quotes in the argument:
       '/p:Target=Data Source=(local)\SQL;Integrated Security=True'
    /p:Target=Data Source=(local)\SQL;Integrated Security=True

使用反引号转义所有PowerShell特殊字符 (仅作为内联参数完成):
   /p:Target=`"Data Source=`(local`)\SQL`;Integrated Security=True`"
/p:Target=Data` Source=`(local`)\SQL`;Integrated` Security=True
/p:Target=Data Source=(local)\SQL;Integrated Security=True

Escape all PowerShell special characters with backticks (this can only be done as an in-line argument):
   /p:Target=`"Data Source=`(local`)\SQL`;Integrated Security=True`"
or /p:Target=Data` Source=`(local`)\SQL`;Integrated` Security=True
/p:Target=Data Source=(local)\SQL;Integrated Security=True


完整的命令行示例(使用第二种替代方法):


Full command line example (using the second alternative):

PS> [string[]]$arguments = @(
  '/target:Deploy',
  '/p:UseSandboxSettings=False',
  '/p:TargetDatabase=UpdatedTargetDatabase',
  '/p:TargetConnectionString=\"Data Source=(local)\SQL;Integrate Security=True\"',
  'C:\program files\MyProjectName.dbproj'
)
PS> ./echoargs $arguments


Arg 0 is </target:Deploy>
Arg 1 is </p:UseSandboxSettings=False>
Arg 2 is </p:TargetDatabase=UpdatedTargetDatabase>
Arg 3 is </p:TargetConnectionString="Data Source=(local)\SQL;Integrate Security=True">
Arg 4 is <C:\program files\MyProjectName.dbproj>


当人们在旧的cmd系统和PowerShell之间移动时,调用本机命令会产生很多麻烦(几乎与用逗号分隔参数"一样多.).

Calling native commands is something that crops up quite a bit as folks move between the legacy cmd system and PowerShell (almost as much as the "separating parameters with commas" gotcha ;).

在这里,我已经尝试并总结了我在PowerShell(v2和v3)中关于命令调用的所有知识,以及我可以使用的所有示例和参考.

I've tried and sum up everything I know on the subject of command invocation in PowerShell (v2 and v3) here, along with all the examples and references I can muster.


1.1)简单来说,对于位于环境路径中的可执行文件,可以直接调用该命令,就像调用PowerShell cmdlet一样. >

1.1) At its simplest, for an executable located in the environment path, the command can be called directly, just as you would call a PowerShell cmdlet.

PS> Get-ItemProperty echoargs.exe -Name IsReadOnly
...
IsReadOnly   : True    

PS> attrib echoargs.exe
A    R       C:\Users\Emperor XLII\EchoArgs.exe


1.2)在环境路径之外,对于特定目录(包括当前目录)中的命令,可以使用命令的完整或相对路径.想法是让操作员显式声明我要调用文件",而不是让碰巧具有相同名称的任意文件在其位置运行(


1.2) Outside of the environment path, for commands in a specific directory (including the current one), the full or relative path to the command can be used. The idea is to have the operator declare explicitly "I want to invoke this file", rather than let an arbitrary file that happened to have the same name get run in its place (see this question for more info on PowerShell security). Failing to use a path when it is required will result in a "term is not recognized" error.

PS> echoargs arg
The term 'echoargs' is not recognized as the name of a cmdlet, function, script file,
 or operable program...

PS> ./echoargs arg
Arg 0 is <arg>

PS> C:\Windows\system32\attrib.exe echoargs.exe
A    R       C:\Users\Emperor XLII\EchoArgs.exe


1.3)如果路径包含特殊字符,则可以使用呼叫操作符或转义字符.例如,以数字开头或位于包含空格的目录中的可执行文件.


1.3) If a path contains special characters, the call operator or escape character can be used. For example, an executable starting with a number, or located in a directory containing a space.

PS> $env:Path
...;C:\tools\;...

PS> Copy-Item EchoArgs.exe C:\tools\5pecialCharacter.exe
PS> 5pecialCharacter.exe special character
Bad numeric constant: 5.

PS> & 5pecialCharacter.exe special character
Arg 0 is <special>
Arg 1 is <character>

PS> `5pecialCharacter.exe escaped` character
Arg 0 is <escaped character>


PS> C:\Users\Emperor XLII\EchoArgs.exe path with spaces
The term 'C:\Users\Emperor' is not recognized as the name of a cmdlet, function,
 script file, or operable program...

PS> & 'C:\Users\Emperor XLII\EchoArgs.exe' path with spaces
Arg 0 is <path>
Arg 1 is <with>
Arg 2 is <spaces>

PS> C:\Users\Emperor` XLII\EchoArgs.exe escaped` path with` spaces
Arg 0 is <escaped path>
Arg 1 is <with spaces>


2.1):当您不是交互式地键入命令,而是将路径存储在变量中时,调用操作符还可用于调用变量中命名的命令.

2.1) When you are not typing out a command interactively, but instead have the path stored in a variable, the call operator can also be used to invoke the command named in a variable.

PS> $command = 'C:\Users\Emperor XLII\EchoArgs.exe'
PS> $command arg
Unexpected token 'arg' in expression or statement.

PS> & $command arg
Arg 0 is <arg>


2.2)传递给命令的参数也可以存储在变量中. 变量中的参数可以单独传递或以数组形式传递.对于包含空格的变量,PowerShell会自动转义空格,以便本机命令将其视为单个参数. (请注意,调用运算符将​​第一个值视为命令,将其余值视为参数;这些参数不应与命令变量组合.)


2.2) The arguments passed to a command can also be stored in variables. Arguments in variables can be passed individually, or in an array. For variables containing spaces, PowerShell will automatically escape the spaces so that the native command sees it as a single argument. (Note that the call operator treats the first value as the command and the remaining values as arguments; the arguments should not be combined with the command variable.)

PS> $singleArg = 'single arg'
PS> $mushedCommand = "$command $singleArg"
PS> $mushedCommand
C:\Users\Emperor XLII\EchoArgs.exe single arg

PS> & $mushedCommand
The term 'C:\Users\Emperor XLII\EchoArgs.exe single arg' is not recognized as the
 name of a cmdlet, function, script file, or operable program...

PS> & $command $singleArg
Arg 0 is <single arg>

PS> $multipleArgs = 'multiple','args'
PS> & $command $multipleArgs
Arg 0 is <multiple>
Arg 1 is <args>


2.3)数组格式对于为本机命令建立动态参数列表特别有用.对于每个要被识别为不同参数的参数,重要的是将参数存储在数组中变量,而不仅仅是将它们混成一串. (请注意,常见缩写$args是PowerShell中的一个自动变量,它可能导致保存在其中的值被覆盖;相反,最好使用诸如$msbuildArgs之类的描述性名称,以避免命名冲突.)


2.3) The array format is especially useful for building up a dynamic list of arguments for a native command. For each argument to be recognized as a distinct parameter, it is important that the arguments get stored in an array variable, and not just munged together in one string. (Note that the common abbreviation $args is an automatic variable in PowerShell, which can cause values saved in it to get overwritten; instead, it is better to use a descriptive name like $msbuildArgs to avoid the naming conflict.)

PS> $mungedArguments = 'initial argument'
PS> $mungedArguments += 'second argument'
PS> $mungedArguments += $(if( $someVariable ) { 'dynamic A' } else { 'dynamic B' })
PS> ./echoargs $mungedArguments
Arg 0 is <initial argumentsecond argumentdynamic B>

PS> $arrayArguments = @('initial argument')
PS> $arrayArguments += 'second argument'
PS> $arrayArguments += $(if( $someVariable ) { 'dynamic A' } else { 'dynamic B' })
PS> ./echoargs $arrayArguments
Arg 0 is <initial argument>
Arg 1 is <second argument>
Arg 2 is <dynamic B>


2.4)另外,对于脚本,函数,cmdlet等,PowerShell v2可以使用称为"splatting"的技术发送哈希表中包含的命名参数,而不必担心参数顺序.这不适用于本机命令,本机命令不参与PowerShell对象模型,而只能处理字符串值.


2.4) Also, for scripts, functions, cmdlets, and the like, PowerShell v2 can send named arguments contained in a hashtable using a technique called "splatting", without having to worry about parameter order. This does not work with native commands, which do not participate in the PowerShell object model and can only handle string values.

PS> $cmdletArgs = @{ Path = 'EchoArgs.exe'; Name = 'IsReadOnly' }
PS> $cmdlet = 'Get-ItemProperty'
PS> & $cmdlet $cmdletArgs     # hashtable object passed to cmdlet
Cannot find path 'C:\Users\Emperor XLII\System.Collections.Hashtable'...

PS> & $cmdlet @cmdletArgs     # hashtable values passed to cmdlet
...
IsReadOnly   : True

PS> ./echoargs @cmdletArgs
Arg 0 is <Name>
Arg 1 is <IsReadOnly>
Arg 2 is <Path>
Arg 3 is <EchoArgs.exe>


3.1)对于简单的参数,用于本机命令的自动转义通常就足够了.但是,对于括号,美元符号,空格等,PowerShell使用的字符需要转义后才能按原样发送给本机命令,而无需解析器对其进行解释.可以使用反引号转义符`或将参数放在单引号字符串中来完成.

3.1) For simple arguments, the automatic escaping used for native commands is generally sufficient. However, for parenthesis, dollar signs, spaces, and such, characters used by PowerShell need to be escaped to be sent as-is to native commands, without having them interpreted by the parser. This can be done with the backtick escape character, `, or by putting the argument inside a single-quote string.

PS> ./echoargs money=$10.00
Arg 0 is <money=.00>

PS> ./echoargs money=`$10.00
Arg 0 is <money=$10.00>


PS> ./echoargs value=(spaces and parenthesis)
The term 'spaces' is not recognized as the name of a cmdlet, function, script file,
 or operable program...

PS> ./echoargs 'value=(spaces and parenthesis)'
Arg 0 is <value=(spaces and parenthesis)>


3.2)不幸的是,当涉及到双引号时,这并不是那么简单.作为本机命令的参数处理的一部分,PowerShell处理器尝试对参数中的所有双引号进行规范化,以便将参数的内容(无引号)作为单个值传递给本机命令.解析后,本机命令参数处理是作为单独的步骤进行的,因此常规转义对双引号无效.只能使用转义的单引号或反斜杠的双引号.


3.2) Unfortunately, this is not so simple when double quotes are involved. As part of argument processing for native commands, the PowerShell processor attempts to normalize all double quotes in an argument so that the contents of the argument, sans quotes, is passed as a single value to the native command. The native command parameter processing occurs as a separate step after parsing, so normal escaping will not work for double quotes; only escaped single quotes, or backslash-escaped double quotes can be used.

PS> ./echoargs value="double quotes"
Arg 0 is <value=double quotes>

PS> ./echoargs 'value="string double quotes"'
Arg 0 is <value=string>
Arg 1 is <double>
Arg 2 is <quotes>

PS> ./echoargs value=`"escaped double quotes`"
Arg 0 is <value=escaped double quotes>

PS> ./echoargs 'value=\"backslash escaped double quotes\"'
Arg 0 is <value="backslash escaped double quotes">


PS> ./echoargs value='single quotes'
Arg 0 is <value=single quotes>

PS> ./echoargs "value='string single quotes'"
Arg 0 is <value='string single quotes'>

PS> ./echoargs value=`'escaped` single` quotes`'
Arg 0 is <value='escaped single quotes'>


3.3) PowerShell v3添加了新的停止分析符号--%(请参阅
about_Parsing ).当在复杂参数之前使用时, --%将按原样传递参数,而不进行任何解析或变量扩展,除了类似于cmd的%ENVIRONMENT_VARIABLE%.


3.3) PowerShell v3 added a new stop-parsing symbol --% (see about_Parsing). When used before complicated arguments, --% will pass arguments as-is without any parsing or variable expansion, except for cmd-like %ENVIRONMENT_VARIABLE% values.

PS> ./echoargs User:"$env:UserName" "Hash"#555
Arg 0 is <User:Emperor XLII>
Arg 1 is <Hash>

PS> ./echoargs User: "$env:UserName" --% "Hash"#555
Arg 0 is <User:Emperor XLII>
Arg 1 is <Hash#555>

PS> ./echoargs --% User: "%USERNAME%" "Hash"#555
Arg 0 is <User:Emperor XLII>
Arg 1 is <Hash#555>

也可以通过在字符串中传递停止分析符号来减免代表多个参数的单个字符串

This can also be used to de-munge a single string representing multiple arguments, by passing the stop-parsing symbol in a string (although the best practice is to not munge arguments in the first place).

PS> $user = 'User:"%USERNAME%"'
PS> $hash = 'Hash#' + $hashNumber
PS> $mungedArguments = $user,$hash -join ' '
PS> ./echoargs $mungedArguments
Arg 0 is <User:%USERNAME% Hash#555>

PS> ./echoargs --% $mungedArguments
Arg 0 is <$mungedArguments>

PS> ./echoargs '--%' $mungedArguments
Arg 0 is <User:Emperor XLII>
Arg 1 is <Hash#555>


有两个用于调试PowerShell传递给本机命令的参数的关键工具.

There are two key tools for debugging the arguments PowerShell passes to native commands.

4.1),第一个是 EchoArgs.exe ,它是

4.1) The first is EchoArgs.exe, a console application from the PowerShell Community Extensions that simply writes back the arguments passed to it between angle brackets (as shown in the examples above).

4.2)第二个是 Trace-Command ,该cmdlet可以显示有关PowerShell如何处理管道的许多详细信息.特别是,NativeCommandParameterBinder跟踪源将显示PowerShell接收并传递给本机命令的内容.

4.2) The second is Trace-Command, a cmdlet that can show many details of how PowerShell processes a pipeline. In particular, the NativeCommandParameterBinder trace source will show what PowerShell receives and passes on to a native command.

PS> Trace-Command *NativeCommand* { ./echoargs value="double quotes" } -PSHost
DEBUG: NativeCommandParameterBinder : Raw argument string:  "value=double quotes"
DEBUG: NativeCommandParameterBinder : Argument 0: value=double quotes

PS> Trace-Command *NativeCommand* { ./echoargs 'value="double quotes"' } -PSHost
DEBUG: NativeCommandParameterBinder : Raw argument string:  "value="double quotes""
DEBUG: NativeCommandParameterBinder : Argument 0: value=double
DEBUG: NativeCommandParameterBinder : Argument 1: quotes

PS> Trace-Command *NativeCommand* { ./echoargs value=`"double quotes`" } -PSHost
DEBUG: NativeCommandParameterBinder : Raw argument string:  value="double quotes"
DEBUG: NativeCommandParameterBinder : Argument 0: value=double quotes

PS> Trace-Command *NativeCommand* { ./echoargs 'value=\"double quotes\"' } -PSHost
DEBUG: NativeCommandParameterBinder : Raw argument string:  "value=\"double quotes\""
DEBUG: NativeCommandParameterBinder : Argument 0: value="double quotes"


文章

  • 2012-01-02 - PowerShell V3 CTP2 Provides Better Argument Passing to EXEs
  • 2011-03-10 - The problem with calling legacy/native apps from PowerShell
  • 2010-11-04 - Escaping Spaces
  • 2010-02-01 - The trials and tribulations of using MSDeploy with PowerShell
  • 2008-10-17 - Executing commands which require quotes and variables is practically impossible [Connect]
  • 2006-05-15 - Cannot enter an argument containing double quotes [Connect]

问题

  • 2013-09-11 - powershell executing external command not accepting parameter
  • 2013-02-20 - Parameters with double quotes are not properly passed to Scriptblock by ArgumentList
  • 2013-01-02 - ERROR: Description = Invalid query
     
  • 2012-09-18 - How can I execute an external program with parameters in PowerShell?
  • 2012-09-10 - Invoke executable (w/parameters) from powershell script
  • 2012-08-16 - How do I pass a property value containing a semicolon on the MSBuild command line when running it from PowerShell?
  • 2012-08-08 - Call ruby script from powershell
  • 2012-08-01 - Brackets or quotation marks breaking a powershell command
  • 2012-07-13 - Problems using powershell to perform a source safe get by label
  • 2012-06-13 - Missing argument -m using svn at windows powershell
  • 2012-06-01 - Powershell command line argument with spaces and curly brackets?
  • 2012-04-18 - Spaced paths, msbuild, and psake
  • 2012-04-12 - Make Power shell ignore semicolon
  • 2012-03-08 - Simple Powershell Msbuild with parameter fails
  • 2012-02-10 - Quote Madness in Powershell
  • 2012-01-18 - Powershell: run msiexec with dynamically created parameters
  • 2012-01-18 - PowerShell's call operator (&) syntax and double-quotes
  • 2012-01-16 - PowerShell - passing calculated paths with spaces
  • 2012-01-09 - powershell: script to start a program with parameters?
     
  • 2011-12-20 - Powershell - calling icacls with parantheses included in parameters
  • 2011-12-15 - Msbuild log doesn't work when executed through powershell
  • 2011-12-06 - Pass parameters to InstallUtil from Powershell
  • 2011-11-23 - Executing an exe with arguments using Powershell
  • 2011-11-08 - Powershell remove quotes when start process
  • 2011-09-16 - Commands executed in PowerShell with variables surrounded in quotes fail. Why?
  • 2011-07-25 - Powershell parsing quotes strangely (includes a short analysis of quote parsing in one of the answers)
  • 2011-07-15 - powershell stripping double quotes from command line arguments
  • 2011-06-14 - In Powershell, how do you execute an arbitrary native command from a string?
  • 2011-06-03 - Powershell call msbuild with nested quotation marks
  • 2011-05-13 - powershell - passing parameters to exe
  • 2011-03-02 - Why does this PowerShell script fail to execute this external command properly?
  • 2011-01-09 - Executing an EXE file using powershell script
     
  • 2010-12-13 - Command-line arguments to an exe
  • 2010-10-08 - What is up with this PowerShell command line quoting/escaping?
  • 2010-10-05 - Running an exe using powershell from a directory with spaces in it
  • 2010-08-28 - Executing a Command stored in a Variable from Powershell
  • 2010-08-17 - How do you call msdeploy from powershell when the parameters have spaces?
  • 2010-04-12 - How to suppress quotes in Powershell commands to executables
  • 2010-01-26 - powershell sending multiple parameter to a external command
     
  • 2009-11-04 - How to run exe in powershell with parameters with spaces and quotes
  • 2009-03-16 - PowerShell - Start-Process and Cmdline Switches
  • 2009-01-14 - How to escape command line arguments on Powershell?

这篇关于Powershell调用带嵌套引号的msbuild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆