AWS & PoweShell - How do I go through a text file from within a script

FikreDeneke used Ask the Experts™
How can I run Script 2 by it's self? Script 1 is a cmd file, but what do I need to delclare in Script 2 to make it a valid script?

Script 1 pulls out all the snapshots (their ID) for a volume to a text file (snaps.txt)
aws ec2 describe-snapshots --filters Name=volume-id,Values=vol-123456 --query Snapshots[*].{ID:SnapshotId} --output text >c:\temp\snaps.txt

Script 2 should go through the text file snaps.txt and tag all the snapshots.
for /F %i in (c:\temp\snaps.txt) do aws ec2 create-tags --resources %i --tags Key=Environment,Value=ABC123

I ran the file snapscript01.cmd to create the list of snapshots in a text file which worked. Awesome. What type of file do I need to run through that output file snaps.txt so it can loop it? I created a cmd and bat with just the command text and it didn't work. No error. I don't want to touch the existing backup script. I want a new script.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dustin SaundersCo-Founder and Chief Architect
Top Expert 2016

Is that the complete Script1?  

What do the contents of snaps.txt look like (sensitive data replaced with something)
@echo off 


@call aws ec2 describe-snapshots --filters Name=volume-id,Values=%VOLUME% --query Snapshots[*].{ID:SnapshotId} --output text > %WORKFILE%

for /F %%i in (%WORKFILE%) do ( 
	echo tagging %%i.....
	@call aws ec2 create-tags --resources %%i --tags Key=Environment,Value=%ENVIRONMENT%

Open in new window

FikreDenekeAWS Cloud Engineer


Shalomc, thanks for your response. Did I properly give you credit by clicking the thumbs up? Let me ask you, what is the difference between your script and the one line script below that I recently found? I ran the script below with a cmd extension.

for /F %%i in (c:\temp\snapshots.txt) do aws ec2 create-tags --resources %%i --tags Key=Environment,Value=ABC123
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

FikreDenekeAWS Cloud Engineer


Dustin Saunders, it is a text file with one column with snapshot ids such as the examples below?

FikreDenekeAWS Cloud Engineer


1. Can we combine these two scripts? They work by themselves, but I can't combine successfully. I want to ideally get the latest snapshot, but any snapshots after 01-01-2018. The current method is grabbing all snapshots which exceed 20,000 and dumps it to a text file, but takes all day to apply the tag. All current snapshots have tags (still running) so I just need to tag most recent or anything after 01-01-2018.

aws ec2 describe-snapshots --filters Name=volume-id,Values=vol-7e82333f --query Snapshots[*].{ID:SnapshotId} --output text >>c:\\AWS\\all_HST-2_snapshots.txt

aws ec2 describe-snapshots --query 'Snapshots[?StartTime >= `2018-01-01`].{id:SnapshotId}'
Your original script had the wrong number of percent signs :D

I am not sure that I follow your last comment. How do you mean to combine the scripts?
PberSolutions Architect

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: shalomc (https:#a42429804)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial