jas*_*ark 8 powershell escaping
我正在尝试从Powershell调用另一个应用程序(Beyond Compare),它在典型的命令行中需要@:
C:\deploy>bcompare @static.diff
Run Code Online (Sandbox Code Playgroud)
我找到了Powershell的Invoke-Expression,但是当我尝试以下操作时它给了我一个错误:
PS C:\deploy>Invoke-Expression "bcompare @static.diff"
Invoke-Expression : Cannot expand the splatted variable '@static'. Splatted variables
cannot be used as part of a property or array expression. Assign the result of the
expression to a temporary variable then splat the temporary variable instead.
At line:1 char:18
+ Invoke-Expression <<<< "bcompare @static.diff"
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : NoPropertiesInSplatting,Microsoft.PowerShell.Comands.InvokeExpressionCommand
Run Code Online (Sandbox Code Playgroud)
我不能让@在这里正确逃脱.我已经尝试了`,@ @,将部分命令放在一个临时变量中,但是没有一个能够完成这个操作.
Joe*_*oey 11
bcompare '@static.diff'
Run Code Online (Sandbox Code Playgroud)
如有疑问,请将其放入字符串:-)
PS Home:\> args '@static.diff'
argv[0] = "C:\Users\Joey\Batches\args.cmd"
argv[1] = @static.diff
Run Code Online (Sandbox Code Playgroud)
您需要两次转义,因为您要经历两个层次的解释。只有一个`无效,因为在字符串创建期间会对其进行解析。
Invoke-Expression "bcompare ``@static.diff"
Run Code Online (Sandbox Code Playgroud)
或如乔伊所说。
Invoke-Expression "bcompare '@static.diff'"
Run Code Online (Sandbox Code Playgroud)