Page 1 of 1

Anyone know Powershell? Querying AWS with PS

Posted: Wed Jan 11, 2017 4:30 pm
by psyolent
I can find my way around this shit but, this has me stuffed.
Am trying to list out reserved instances in AWS using th eGet-EC2ReservedInstance cmdlet, but, no dice. at all.
The filter directive has as applicable value where state can be payment-pending | active | payment-failed | retired etc

Get-EC2ReservedInstance -Filter state=Active -ProfileName myProfile -Region ap-southeast-2

Get-EC2ReservedInstance : The filter 'State=Active' is invalid
At line:1 char:1
+ Get-EC2ReservedInstance -Filter State=Active -ProfileName myProfile -Region ap-s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Amazon.PowerShe...dInstanceCmdlet:GetEC2ReservedInstanceCmdlet) [Get-EC2
ReservedInstance], InvalidOperationException
+ FullyQualifiedErrorId : Amazon.EC2.AmazonEC2Exception,Amazon.PowerShell.Cmdlets.EC2.GetEC2ReservedInstanceCmdlet

I know state is of 'string' type ; and no number of permutations / combinations has me able to return a successful result. I know state is valid as when i run the command without =Active, it returns nothing ....

Advice appreciated.

Re: Anyone know Powershell? Querying AWS with PS

Posted: Wed Jan 11, 2017 4:56 pm
by psyolent
Get-EC2ReservedInstance -ProfileName myProfile -Region ap-southeast-2 | ?{$_.state -eq 'active'};

pity its not using the implicit 'filter' though.

Re: Anyone know Powershell? Querying AWS with PS

Posted: Thu Jan 12, 2017 8:24 am
by psyolent
nup, couldn't make -Filter play
landed on Get-EC2ReservedInstance -Region ap-southeast-2 -ProfileName myProfile | select InstanceType,InstanceCount,State,End | ?{$_.state -eq 'active'} | sort End,InstanceType | Out-File ${filepath}RIList.csv

Re: Anyone know Powershell? Querying AWS with PS

Posted: Thu Jan 12, 2017 9:24 am
by The1
Never used it before but Amazon has a page here
https://aws.amazon.com/powershell/

Re: Anyone know Powershell? Querying AWS with PS

Posted: Thu Jan 12, 2017 12:08 pm
by psyolent
yeah, like with alot of AWS stuff its pretty light on mate. i've figured it out and have another thing emailing me now so all good ; for reference ;

# *****************************************************************************************************************
# ****** Weekly Run script to check for users > 90 days old in AWS ******
# ****** ******
# ****** Created By Greg W 12/01/2017 ******
# ****** Modified By ******
# ****** Runs from C:\AWS\PS\ folder on ******
# ****** Scheduled Task to run weekly ******
# ****** AWS Logon Variables set using Set-AWSCredentials -AccessKey xyz -SecretKey abc -StoreAs -Whatever ******
# ****** These must be set on a per environment basis on the server where this PS runs. ******
# *****************************************************************************************************************


# Global Variables
$filepath = "C:\AWS\Reports\"
$mailtoo = "recipient@whatever.com"
$mailfrom = "sender@whatever.com"
$smtpserver = "smtpserver"

#DEV Users not logged in > 90 days
#Local Variable for DEV
$mailsubject = "DEV Users not logged in for greater than 90 days"
Get-IAMUsers -ProfileName Dev |select UserName,PasswordLastUsed | Where {$_.UserName -inotmatch 'svc*' -and $_.PasswordLastUsed -lt ((get-date).adddays(-90))} | sort PasswordLastUsed,UserName | Out-File ${filepath}DevUsers.txt
Send-MailMessage -To $mailtoo -From $mailfrom -SmtpServer $smtpserver -Subject $mailsubject -Attachments ${filepath}DevUsers.txt

Re: Anyone know Powershell? Querying AWS with PS

Posted: Thu Jan 12, 2017 6:07 pm
by psyolent
confirmed - deets on this stuff VERY thin on the ground. will have to write a blog.

Re: Anyone know Powershell? Querying AWS with PS

Posted: Fri Jan 13, 2017 6:45 am
by psyolent
Landed here

# Global Variables
$filepath = "C:\AWS\Reports\"
$mailtoo = "recipient@whatever.com"
$mailfrom = "sender@whatever.com"
$smtpserver = "smtpserver"
$env_array = "ENV1","ENV2","ENV3"
$env_item = ""

foreach ($env_item in $env_array) {
$mailsubject = $env_item+" Users not logged in for greater than 90 days"
Get-IAMUsers -ProfileName $env_item |select UserName,PasswordLastUsed | Where {$_.UserName -inotmatch 'svc*' -and $_.PasswordLastUsed -lt ((get-date).adddays(-90))} | sort PasswordLastUsed,UserName | Out-File ${filepath}"$env_item Users.txt"
Send-MailMessage -To $mailtoo -From $mailfrom -SmtpServer $smtpserver -Subject $mailsubject -Attachments ${filepath}"$env_item Users.txt"
}