简短版本
如何从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\"'
→ /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
完整的命令行示例(使用第二种选择):
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
参数 0 是 </target:Deploy>
参数 1 是 </p:UseSandboxSettings=False>
参数 2 是 </p:TargetDatabase=UpdatedTargetDatabase>
参数 3 是 </p:TargetConnectionString="Data Source=(local)\SQL;Integrate Security=True">
参数 4 是 <C:\program files\MyProjectName.dbproj>
长版
在从传统的cmd系统转换到PowerShell时,调用本地命令是经常出现的事情(几乎和“用逗号分隔参数”一样多)。
我已经尽力总结了我所知道的有关PowerShell(v2和v3)中命令调用的所有内容,并提供了所有我能想到的例子和参考资料。
1) 直接调用本地命令
1.1) 最简单的情况是,如果一个可执行文件位于环境路径中,那么就可以直接调用该命令,就像调用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) 在环境路径之外,可以使用完整或相对路径执行特定目录中(包括当前目录中)的命令。其思想是让操作员明确声明“我想调用这个文件”,而不是让任意具有相同名称的文件运行在其原本的位置上(有关PowerShell安全性的更多信息,请参见此问题)。在需要使用路径时未使用路径将导致“术语未被识别”错误。
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
如果路径包含特殊字符,可以使用调用运算符或转义字符。例如,以数字开头的可执行文件,或位于包含空格的目录中。
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) 间接调用本地命令
2.1) 当您不是交互式地输入命令,而是将路径存储在变量中时,调用运算符也可以用于调用存储在变量中的命令。
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会自动转义空格,以便原生命令将其视为单个参数。(请注意,调用运算符将第一个值视为命令,将剩余的值视为参数;参数不应与命令变量组合。)
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
来避免命名冲突。)
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对象模型并且只能处理字符串值的本机命令无效。
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) 调用带有复杂参数的本地命令
3.1) 对于简单的参数,通常使用本地命令的自动转义就足够了。但是,对于括号、美元符号、空格等 PowerShell 使用的字符需要进行转义才能原样发送给本地命令,而不是被解释器解释。可以使用反引号转义字符`
或将参数放在单引号字符串中来实现此操作。
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处理器尝试规范化参数中的所有双引号,以便将参数内容(不带引号)作为单个值传递给本机命令。本机命令参数处理发生在解析之后的单独步骤中,因此
普通的转义对于双引号不起作用;只能使用转义的单引号或反斜杠转义的双引号。
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%
值。
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>
这也可以用来解开一个表示多个参数的字符串,只需将停止解析符号作为字符串
∗传递即可(尽管最好的做法是一开始就不要混淆参数)。
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>
4) 调试本机命令
有两个关键工具可以调试PowerShell传递给本机命令的参数。
4.1) 第一个是EchoArgs.exe
,它是PowerShell社区扩展中的控制台应用程序,只需将传递给它的参数写回尖括号之间(如上面的示例所示)。
4.2) 第二个是Trace-Command
,这是一个可显示PowerShell处理管道的许多细节的cmdlet。特别地,NativeCommandParameterBinder
跟踪源将显示PowerShell接收并传递给本机命令的内容。
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"
其他资源
文章
问题