Avatar of FikreDeneke
FikreDenekeFlag for United States of America

asked on 

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.
PowershellAWS

Avatar of undefined
Last Comment
Pber
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Is that the complete Script1?  

What do the contents of snaps.txt look like (sensitive data replaced with something)
ASKER CERTIFIED SOLUTION
Avatar of Shalom Carmel
Shalom Carmel
Flag of Israel image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of FikreDeneke
FikreDeneke
Flag of United States of America image

ASKER

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
Avatar of FikreDeneke
FikreDeneke
Flag of United States of America image

ASKER

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


aaa111
bbb222
ccc333
Avatar of FikreDeneke
FikreDeneke
Flag of United States of America image

ASKER

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}'
Avatar of Shalom Carmel
Shalom Carmel
Flag of Israel image

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?
Avatar of Pber
Pber
Flag of Canada image

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.

Pber
Experts-Exchange Cleanup Volunteer
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo