Powershell:带有嵌入双引号的gpg命令参数
我正在尝试从Powershell脚本调用gpg2。我需要用嵌入的引号传递参数,但当我直接查看echoargs或可执行文件的结果时,会出现一些非常奇怪的行为Powershell:带有嵌入双引号的gpg命令参数,powershell,gnupg,Powershell,Gnupg,我正在尝试从Powershell脚本调用gpg2。我需要用嵌入的引号传递参数,但当我直接查看echoargs或可执行文件的结果时,会出现一些非常奇怪的行为 $Passphrase = "PassphraseWith!$#" #don't worry, real passphrase not hardcoded! $Filename = "\\UNC\path\with\a space\mydoc.pdf.pgp" $EncyptedFile = $Filename -replace "\\",
$Passphrase = "PassphraseWith!$#" #don't worry, real passphrase not hardcoded!
$Filename = "\\UNC\path\with\a space\mydoc.pdf.pgp"
$EncyptedFile = $Filename -replace "\\", "/"
$DecryptedFile = $EncyptedFile -replace ".pgp" , ""
$args = "--batch", "--yes", "--passphrase `"`"$PGPPassphrase`"`"", "-o `"`"$DecryptedFile`"`"", "-d `"`"$EncyptedFile`"`""
& echoargs $args
& gpg2 $args
gpg要求我对密码短语使用双引号,因为它有符号,而对路径使用双引号是因为有空格(当我直接从命令提示符运行单个命令示例时,确认了这一点)。此外,gpg希望UNC路径具有正向斜杠(已确认这也有效)
正如您所看到的,我试图用成对的转义双引号来包装密码短语和文件路径,因为echoargs似乎表明外部引号正在被剥离。以下是我从echoargs获得的信息:
Arg 0 is <--batch>
Arg 1 is <--yes>
Arg 2 is <--passphrase "PassphraseWith!$#">
Arg 3 is <-o "//UNC/path/with/a space/mydoc.pdf">
Arg 4 is <-d "//UNC/path/with/a space/mydoc.pdf.pgp">
Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\PSCX\Apps\EchoArgs.exe" --batch --yes "--pass
phrase ""PassphraseWith!$#""" "-o ""//UNC/path/with/a space/mydoc.pdf""" "-d ""//UNC/path/with/a space/mydo
c.pdf.pgp"""
Arg 0为空
arg1是
arg2是
arg3是
arg4是
命令行:
“C:\Program Files(x86)\PowerShell社区扩展\Pscx3\PSCX\Apps\EchoArgs.exe”--批处理--是”--通过
短语“PassphraseWith!$#”“-o”“//UNC/path/with/a space/mydoc.pdf”“-d”“//UNC/path/with/a space/mydo
c、 pdf.pgp
但是,gpg2给出了以下结果(无论是直接从ISE运行还是从PS运行):
gpg2.exe:gpg:invalid选项“--passphrase”PassphraseWith$#“”
如果我尝试&gpg2“$args”
将数组转换为字符串,则会得到以下类似结果:
gpg2.exe:gpg:invalid option”--批处理--yes--passphrase“PassphraseWith$#“
对此有什么想法吗?@PetSerAl的解决方案:您需要标记标志/参数及其值,因此在数组中分成两个元素:
"--passphrase", "`"$Passphrase`""
未合并为:
"--passphrase `"`"$Passphrase`"`""
请注意,使用反勾号的常规Powershell转义引号在这里可以正常工作。
完整示例如下:
$Passphrase = "PassphraseWith!$#" #don't worry, real passphrase not hardcoded!
$Filename = "\\UNC\path\with\a space\mydoc.pdf.pgp"
$EncyptedFile = $Filename -replace "\\", "/"
$DecryptedFile = $EncyptedFile -replace ".pgp" , ""
$params = "--batch", "--quiet", "--yes", "--passphrase", "`"$Passphrase`"", "-o", "`"$DecryptedFile`"", "-d", "`"$EncyptedFile`""
& echoargs $params
& gpg2 $params
这将是一个荒谬的答案,但如果你的密码中真的有一个金钱符号,你必须使用单引号…@Cole9350即使我从pw中取出符号,我也会得到相同的结果,但是,值得注意的是,在第一行的初始声明中,一个$后跟一个字母数字将需要单引号
${Not$args,因为$args是自动变量}=“--batch”、“--yes”、“--passphrase”、“$PGPPassphrase”、“-o”、“$DecryptedFile”、“-d”、“$EncyptedFile””;echoargs`${不是args,因为$args是自动变量}
@PetSerAl,这太神奇了!FWIW,它似乎并不在乎使用$args(反正我已经改变了),但将问题项拆分为标志和值非常有效!非常感谢!