• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 82
  • Last Modified:

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

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.
1 Solution
Dustin SaundersDirector of OperationsCommented:
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 EngineerAuthor Commented:
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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

FikreDenekeAWS Cloud EngineerAuthor Commented:
Dustin Saunders, it is a text file with one column with snapshot ids such as the examples below?

FikreDenekeAWS Cloud EngineerAuthor Commented:
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 ArchitectCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now