Windows console: run AWS CLI command inside shell command error

0

Hello,

I have installed on Windows 10, the AWS CLI, and try to make a .bat file with this command:

for /f "tokens=*" %%a in ('aws s3api list-objects-v2 --bucket pics-repository --query Contents[*].Key --max-items 10') do set _CmdResult=%%a
@echo result : %_CmdResult%

While this works when the inner command is a Windows one, in the above case I have the error: No Windows console found. Are you running cmd.exe?. I run this version of CLI : aws-cli/2.11.0 Python/3.11.2 Windows/10 exe/AMD64 prompt/off

Is this related to some sort of special environment created by CLI in Windows shell ? Can this work, or no AWS CLI can be ran like this (I had also this error with other CLI commands) ?

Thank you,
Mihai ADAM

asked 9 months ago1121 views
2 Answers
0

here are some ways i have found on another thread related to this:

use the aws.exe executable directly from the command line. For example, you could run the following command to list the objects in the pics-repository bucket:

aws.exe s3api list-objects-v2 --bucket pics-repository --query Contents[*].Key --max-items 10 Another way to work around this issue is to use the Invoke-Expression cmdlet in PowerShell. For example, you could run the following command to list the objects in the pics-repository bucket:

Invoke-Expression "aws s3api list-objects-v2 --bucket pics-repository --query Contents[*].Key --max-items 10"

Finally, you could also use the aws-cli.exe executable to run your batch file. This executable will create a Windows console and execute your batch file in the context of the AWS CLI. However, this executable is not officially supported by AWS, so you may experience some unexpected behaviour.

profile picture
answered 9 months ago
  • Hello,

    To give you a bigger picture of the problem, I could say that I'm trying to pipe some aws commands, but none of the above solutions works. Did I mistake some wording or is the piping impossible with aws cli environment ?

    The solution that I'm looking for, looks something like below:

    Windows cmd: for /f "tokens=" %%a in ('aws s3api list-objects-v2 --bucket pics-repository --query Contents[].Key --max-items 10') do set _CmdResult2=%%a aws s3api get-object-tagging --bucket pics-repository --key %_CmdResult2%

    Windows PS: Invoke-Expression "aws s3api list-objects-v2 --bucket pics-repository --query Contents[*].Key --max-items 2" | "aws s3api get-object-tagging --bucket pics-repository --key Key"

    But none of the above works with the same error: "No Windows console found. Are you running cmd.exe?". Can AWS CLI be used in such a way ?

    Thank you, Mihai

0

Hello,

After some investigations I found out that the solution would be to add the option --no-cli-auto-prompt to the AWS CLI command:

for /f "tokens=*" %%a in ('aws s3api list-objects-v2 --bucket pics-repository --no-cli-auto-prompt --query Contents[*].Key --max-items 10 ') do set _CmdResult=%%a  

It seams that the blocking option in the CLI command (that waits for user input) is not well-understood by Windows cmd.

Have a good day,
Mihai ADAM

answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions