在PowerShell中使用双引号,然后卷曲大括号 [英] Use double quote then curly brace in powershell -Command

查看:757
本文介绍了在PowerShell中使用双引号,然后卷曲大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有几个与此相关的问题,但他们专门处理写入主机。我想要运行像

There are a couple questions related to this on here but they specifically address Write-Host. I want to run something like

powershell.exe -Command "'example.exe' /f`"`{GUID`}`""

只有失败,错误

Missing closing '}' in statement block.
At line:1 char:396
+ $mypid=(get-process EXEName*).id;wait-process -id $mypid;
& `C:\Users\User\AppData\Local\Temp\{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}\Target.exe`
/s /ig``{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX`}` /instance=1  /f3`C:\Recordings`
/f4`uninstall-log.txt` /f1`C:\Users\User\AppData\Local\Temp\`{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX`}\setup.iss`
/f2`C:\Users\User\AppData\Local\Temp\`{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX`}\setup.log` /removeonly <<<< 
    + CategoryInfo          : ParserError: (CloseBraceToken:TokenId) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndCurlyBrace

我有其他字符串以开头,包含 {} (如 /f\"C:\Folder\{GUID}\program.exe,这些不会造成任何麻烦,只是我的论据是大括号与双引号相邻:

I have other strings starting with " and containing {} (like /f"C:\Folder\{GUID}\program.exe" and these don't cause any trouble. It's only my argument where the curly braces are adjacent to the double quotes:

/ig`"`{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX`}`"

在错误消息中,可能或可能不值得注意的是,所有的双引号都已经消失,所以我的/ ig参数留下了两个我相信我的版本是2.0,这是我的实际(修改)命令:

In the error message, it may or may not be noteworthy that all the double quotes are gone so my /ig argument is left with two backticks. I believe my version is 2.0. Here is my actual (modified) command:

powershell -Command "$mypid=(get-process EXEName*).id;wait-process -id
$mypid;& 'C:\Users\User\AppData\Local\Temp\{XXXXXXXX-XXXX-XXXX-XXXX-
XXXXXXXXXXXX}\Target.exe' /s /ig`"`{XXXXXXXX-XXXX-XXXX-XXXX-
XXXXXXXXXXXX`}`" /instance=1  /f3`"C:\Recordings`" /f4`"uninstall-log.txt`"
/f1`"C:\Users\User\AppData\Local\Temp\`{XXXXXXXX-XXXX-XXXX-XXXX-
XXXXXXXXXXXX`}\setup.iss`" 
/f2`"C:\Users\User\AppData\Local\Temp\`{XXXXXXXX-XXXX-XXXX-XXXX-
XXXXXXXXXXXX`}\setup.log`" /removeonly" 

任何人都可以点亮这个?我不知道为什么这是无效的。谢谢!

Can anyone shed some light on this? I don't know why it would be invalid. Thank you!

推荐答案

问题是将-Command参数用于除简单脚本之外的其他任何事情,如花括号和引号在命令提示符之前会被传递给Powershell。你可以通过添加几层转义或者更简单的方式来结合自己,而不是使用-EncodedCommand参数。

The problem is using the -Command parameter for anything other than a simple script you will run into issues where characters such as curly braces and quotes will be misinterpritted by the command prompt before the are they are passed to Powershell. You could could tie yourself in knots by adding several layers of escaping or there is a simplier way - use the -EncodedCommand parametter instead.

对于EncodedCommand,只需要Base64编码您可以使用以下Powershell脚本执行的命令:

For the EncodedCommand you just need to Base64 encode your command which you can do with the following Powershell script:

$command = @'
  # Enter your commands containg curly braces and quotes here
  # As long as you like  
'@
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes) | clip.exe

这将将编码命令复制到剪贴板,然后您需要做的所有使用你的命令是:

This will copy the encoded command to the clipboard, then all you need to do to use your command is type:

powershell.exe -EncodedCommand

..然后粘贴您的命令,以便您最终得到以下内容:

.. and then paste in your command so that you end up with something like the following:

powershell.exe -EncodedCommand IAAgACMAIABFAG4AdABlAHIAIAB5AG8AdQByACAAcwBjAHIAaQBwAHQAIABjAG8AbgB0AGEAaQBuAGcAIABjAHUAcgBsAHkAIABiAHIAYQBjAGUAcwAgAGEAbgBkACAAcQB1AG8AdABlAHMACgAgACAAIwAgAEEAcwAgAGwAbwBuAGcAIABhAHMAIAB5AG8AdQAgAGwAaQBrAGUAIAAgAA==

您现在拥有的东西,是命令提示安全。

You now have something that is command prompt safe.

这篇关于在PowerShell中使用双引号,然后卷曲大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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