简体   繁体   中英

Pass single quoted string within powershell command in aws cli ssm

I am trying to pass this powershell command to 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'"

But i keep getting this error:

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}}''

i tried escaping the single quotes by having two-single quotes together '' but it still shows the error:

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: i ran it in jenkins

I found the solution!

$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

escaping the single quotes with two backslashes \\ and the dollar and brackets with (`) gave the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM