Darren Reddick
Darren Reddick

Reputation: 88

AWS Cli - Query output using an environment variable in powershell

I am trying to query the output of an AWS cli command using an environment variable as the query string. This works fine for me using the AWS Cli in Linux but in Powershell I am having trouble getting the Cli to use the variable in Powershell.

For example - thsi works for me in Linux:

SECGRP="RDP from Home"
aws ec2 describe-security-groups --query \
'SecurityGroups[?GroupName==`'"$SECGRP"'`].GroupId' --output text

If i run this in Powershell:

$SECGRP="RDP from Home"
aws ec2 describe-security-groups --query \
'SecurityGroups[?GroupName==`'"$SECGRP"'`].GroupId' --output text

Error Details:

Bad value for --query SecurityGroups[?GroupName==`: Bad jmespath 
expression: Unclosed ` delimiter:
SecurityGroups[?GroupName==`
                       ^

I have tried a few combinations of quotes inisde the query expression but either get errors or no output.

I have also run the following to demonstrate i can get the correct output using Powershell (but not using a variable):

aws ec2 describe-security-groups --query \
'SecurityGroups[?GroupName==`RDP from Home`].GroupId' --output text

Upvotes: 2

Views: 4598

Answers (1)

Mark Wragg
Mark Wragg

Reputation: 23395

Try this:

$SECGRP="RDP from Home"
aws ec2 describe-security-groups --query "SecurityGroups[?GroupName=='$SECGRP'].GroupId" --output text

Upvotes: 3

Related Questions