当命令行包含空格时,带有参数的Powershell中的AWS CLI AWS RunRemoteScript返回错误



我的目标是使用PowerShell自动化一些AWS过程。

其中一个步骤是从远程位置在EC2 Windows机器上执行ps1脚本(在我的例子中是S3(。

问题是脚本必须使用强制参数运行。只要命令包含空格,CLI就会抛出:

aws ssm send-command --region us-west-2 --targets Key=tag:T,Values=V --document-name "AWS-RunRemoteScript" --parameters '{"sourceType":["S3"],"sourceInfo":["{\"path\":\"https://bucketName.s3-us-west-2.amazonaws.com/folder/Install.ps1\"}"],"commandLine":[".\Install.ps1 -Param1 value"]}'

返回

aws : usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
At line:1 char:1
+ aws ssm send-command --region us-west-2 --targets Key=tag:T,Values=V  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (usage: aws [opt....] [parameters]:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
Unknown options: -Param1, value"]}

而在没有CCD_ 2的情况下工作良好。即使在"命令行"的末尾添加空格也会导致问题

... "commandLine":[".\Install.ps1 "]}'... Unknown options: "]}的结果

我怀疑这是某种PowerShell语法/转义问题,因为相同的命令(具有正确的转义(在bash控制台上工作。

参考官方文档,它应该在bashshell上工作,但我找不到powershell的任何工作示例。。。。

以下是bash shell中的工作原理:aws ssm send-command --region us-west-2 --targets Key=tag:T,Values=V --document-name "AWS-RunRemoteScript" --parameters '{"sourceType":["S3"],"sourceInfo":["{"path":"https://bucketNameXX.s3-us-west-2.amazonaws.com/folder/Install.ps1"}"],"commandLine":[".Install.ps1 -Param1 value"]}'

删除了所有转义符,除了双引号洞察"sourceInfo"值的转义符。

$aws ssm send-command --region us-west-2 --targets Key=tag:T,Values=V --document-name "AWS-RunRemoteScript" --parameters '{"sourceType":["S3"],"sourceInfo":["{"path":"https://bucketName.s3-us-west-2.amazonaws.com/folder/Install.ps1"}"],"commandLine":[".Install.ps1 -Param1 value"]}'
{
"Command": {
"CommandId": "f1924381-b50c-4a23-a0b5-xxxxxxxxxxxx",
"DocumentName": "AWS-RunRemoteScript",
"DocumentVersion": "",
"Comment": "",
"ExpiresAfter": 1584737377.038,
"Parameters": {
"commandLine": [
".Install.ps1 -Param1 value"
],
"sourceInfo": [
"{"path":"https://bucketNameXX.s3-us-west-2.amazonaws.com/folder/Install.ps1"}"
],
"sourceType": [
"S3"
]
},
"InstanceIds": [],
"Targets": [
{
"Key": "tag:T",
"Values": [
"V"
]
}
],
"RequestedDateTime": 1584730177.038,
"Status": "Pending",
"StatusDetails": "Pending",
"OutputS3BucketName": "",
"OutputS3KeyPrefix": "",
"MaxConcurrency": "50",
"MaxErrors": "0",
"TargetCount": 0,
"CompletedCount": 0,
"ErrorCount": 0,
"DeliveryTimedOutCount": 0,
"ServiceRole": "",
"NotificationConfig": {
"NotificationArn": "",
"NotificationEvents": [],
"NotificationType": ""
},
"CloudWatchOutputConfig": {
"CloudWatchLogGroupName": "",
"CloudWatchOutputEnabled": false
}
}
}