Powershell:带有嵌入式双引号的 gpg 命令参数
Powershell: gpg command parameters with embedded double quotes
我正在尝试从 Powershell 脚本调用 gpg2。我需要传递带有嵌入式引号的参数,但是当我直接查看 echoargs 或可执行文件的结果时,我得到了一些非常奇怪的行为。
$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 要求我对密码使用双引号,因为它有符号和路径,因为 space(当我直接从命令提示符 运行 一个示例单个命令时确认这有效).此外,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"""
然而,gpg2 给出了以下结果(无论是来自 ISE 的 运行 还是直接 PS):
gpg2.exe : gpg: invalid option "--passphrase "PassphraseWith!$#""
如果我尝试 & gpg2 "$args"
将数组转换为字符串,则会得到以下类似结果:
gpg2.exe : gpg: invalid option "--batch --yes --passphrase "PassphraseWith!$#"
对此有什么想法吗?
@PetSerAl 的解决方案:您需要标记化 flag/parameter 及其值,因此拆分为数组中的两个元素:
"--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
我正在尝试从 Powershell 脚本调用 gpg2。我需要传递带有嵌入式引号的参数,但是当我直接查看 echoargs 或可执行文件的结果时,我得到了一些非常奇怪的行为。
$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 要求我对密码使用双引号,因为它有符号和路径,因为 space(当我直接从命令提示符 运行 一个示例单个命令时确认这有效).此外,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"""
然而,gpg2 给出了以下结果(无论是来自 ISE 的 运行 还是直接 PS):
gpg2.exe : gpg: invalid option "--passphrase "PassphraseWith!$#""
如果我尝试 & gpg2 "$args"
将数组转换为字符串,则会得到以下类似结果:
gpg2.exe : gpg: invalid option "--batch --yes --passphrase "PassphraseWith!$#"
对此有什么想法吗?
@PetSerAl 的解决方案:您需要标记化 flag/parameter 及其值,因此拆分为数组中的两个元素:
"--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