0

我正在尝试将此 powershell 命令传递给 aws ssm:

$CMD = "docker image inspect $(docker inspect $(docker ps --filter ancestor=.dkr.ecr.us-east-1.amazonaws.com/service:latest -q) --format '{{.Image}}') --format '{{.RepoDigests}}'"
aws ssm send-command --document-name "AWS-RunPowerShellScript" --targets "Key=tag:group,Values=value" --parameters commands="'$CMD'"

但我不断收到此错误:

aws : 
At C:\jenkins\workspace\Test_Patch_Update@tmp\durable-4179e3f3\powershellScript.ps1:4 char:33
+ ...             aws ssm send-command --document-name "AWS-RunPowerShellSc ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
Error parsing parameter '--parameters': Expected: ',', received: '{' for input:
commands='docker image inspect  $(docker inspect $(docker ps --filter ancestor=dkr.ecr.us-east-1.amazonaws.com/service:latest -q) --format '{{.Image}}') 
--format '{{.RepoDigests}}''

我尝试通过将两个单引号放在一起来转义单引号,''但它仍然显示错误:

aws : 
At C:\jenkins\workspace\Test_Patch_Update@tmp\durable-521ed40a\powershellScript.ps1:4 char:33
+ ...             aws ssm send-command --document-name "AWS-RunPowerShellSc ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : NotSpecified: (:String) [], RemoteException
   + FullyQualifiedErrorId : NativeCommandError

Error parsing parameter '--parameters': Expected: ',', received: ''' for input:
commands='docker image inspect  $(docker inspect $(docker ps --filter ancestor=dkr.ecr.us-east-1.amazonaws.com/service:latest -q) --format ''{{.Image}}'') 
--format ''{{.RepoDigests}}'''

PS:我在詹金斯运行它

4

1 回答 1

0

我找到了解决方案!

$CMD = "docker image inspect `$`(docker inspect `$`(docker ps --filter ancestor=dkr.ecr.us-east-1.amazonaws.com/service:latest -q`) --format \\'{{.Image}}\\'`) --format \\'{{.RepoDigests}}\\'"
                        aws ssm send-command --document-name "AWS-RunPowerShellScript" --targets "Key=tag:Patch Group,Values=stage-ecs-windows" --parameters commands="'$CMD'" | ConvertFrom-Json

用两个反斜杠转义单引号,用\\美元和括号 转义(`)输出。

于 2020-07-23T12:08:47.137 回答