PowerShell 在向 AWS CLI 发送命令之前,它会确定是使用典型 PowerShell 还是 CommandLineToArgvW
引用规则来解释您的命令。PowerShell 使用 CommandLineToArgvW
进行处理时,必须用反斜杠 \
对字符进行转义。
有关 PowerShell 中 CommandLineToArgvW
的更多信息,请参阅 Microsoft DevBlogs 上的 What's up with the strange treatment of quotation marks and backslashes by CommandLineToArgvW(CommandLineToArgvW 对引号和反斜杠的奇怪处理是怎么回事)、Microsoft Docs Blog 中的 Everyone quotes command line arguments the wrong way(每个人引用命令行参数的方法都错了)以及 Microsoft Docs 上的 CommandLineToArgvW function(CommandLineToArgvW 函数)。
单引号
单引号 ' '
称为 verbatim
字符串。字符串将完全按照您键入的方式传递给命令,这意味着 PowerShell 变量将不会传递。使用反斜杠 \
对字符进行转义。
PS C:\>
aws ec2 run-instances `
--image-id ami-12345678 `
--block-device-mappings '
[{\"
DeviceName\"
:\"
/dev/sdb\"
,\"
Ebs\"
:{\"
VolumeSize\"
:20,\"
DeleteOnTermination\"
:false,\"
VolumeType\"
:\"
standard\"
}}]'
双引号
双引号 " "
称为 expandable
字符串。变量可以在 expandable
字符串中传递。对于双引号字符串,必须对每个引号使用 `\
进行两次转义,而不是仅使用反引号。反引号对反斜杠进行转义,然后将反斜杠用作 CommandLineToArgvW
流程的转义字符。
PS C:\>
aws ec2 run-instances `
--image-id ami-12345678 `
--block-device-mappings "
[{`\"
DeviceName`\"
:`\"
/dev/sdb`\"
,`\"
Ebs`\"
:{`\"
VolumeSize`\"
:20,`\"
DeleteOnTermination`\"
:false,`\"
VolumeType`\"
:`\"
standard`\"
}}]"
Blob(推荐)
要绕过 JSON 数据输入的 PowerShell 引用规则,请使用 Blob 将 JSON 数据直接传递到 AWS CLI。有关 Blob 的更多信息,请参阅Blob。