Filtering by StartTime with the AWS PowerShell Cmdlets
I was recently trying to figure out how to return an AWS Storage Gateway snapshot by providing a value for the StartTime property and it wasn't easy to say the least so I thought I would share my experience to save others the headache of figuring it out.
Most of the tutorials you'll find online show filtering something similar to this:
1$filter1 = New-Object Amazon.EC2.Model.Filter
2$filter1.Name = 'volume-id'
3$filter1.Value.Add('myvolumeid')
4
5$filter2 = New-Object Amazon.EC2.Model.Filter
6$filter2.Name = 'status'
7$filter2.Value.Add('completed')
8
9Get-EC2Snapshot -Filter $filter1, $filter2 |
10Select-Object -First 1
You could also use multiple hash tables as shown in the following example:
1Get-EC2Snapshot -Filter @(
2 @{
3 name='volume-id'
4 value='myvolumeid'
5 }
6 @{
7 name='status'
8 value='completed'
9 }
10) |
11Select-Object -First 1
Viewing the help for the Filter parameter shows what the valid names are so that part was easy enough to figure out:
1help Get-EC2Snapshot -Parameter filter
You would think that I could simply copy and paste the date that was returned from one of the first two examples and it would return that one snapshot. If it were only that easy:
1Get-EC2Snapshot -Filter @(
2 @{
3 name='volume-id'
4 value='myvolumeid'
5 }
6 @{
7 name='status'
8 value='completed'
9 }
10 @{
11 name='start-time'
12 value='3/10/2015 1:02:20 AM'
13 }
14)
As it turns out, the value for StartTime has to be in the format shown in this example:
1(Get-Date -Date '3/10/2015 1:02:20 AM').ToUniversalTime().ToString('yyyy-MM-ddThh:mm:ss.000Z')
Using that format for the StartTime value returned the one snapshot without issue:
1Get-EC2Snapshot -Filter @(
2 @{
3 name='volume-id'
4 value='myvolumeid'
5 }
6 @{
7 name='status'
8 value='completed'
9 }
10 @{
11 name='start-time'
12 value='2015-03-10T06:02:20.000Z'
13 }
14)
That was easy enough to figure out, right? I'll let you decide the answer to that for yourself.
µ