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 2 3 4 5 6 7 8 9 10 | $filter1 = New-Object Amazon.EC2.Model.Filter $filter1.Name = 'volume-id' $filter1.Value.Add('myvolumeid') $filter2 = New-Object Amazon.EC2.Model.Filter $filter2.Name = 'status' $filter2.Value.Add('completed') Get-EC2Snapshot -Filter $filter1, $filter2 | Select-Object -First 1 |
You could also use multiple hash tables as shown in the following example:
1 2 3 4 5 6 7 8 9 10 11 | Get-EC2Snapshot -Filter @( @{ name='volume-id' value='myvolumeid' } @{ name='status' value='completed' } ) | Select-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:
1 | help 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Get-EC2Snapshot -Filter @( @{ name='volume-id' value='myvolumeid' } @{ name='status' value='completed' } @{ name='start-time' value='3/10/2015 1:02:20 AM' } ) |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Get-EC2Snapshot -Filter @( @{ name='volume-id' value='myvolumeid' } @{ name='status' value='completed' } @{ name='start-time' value='2015-03-10T06:02:20.000Z' } ) |
That was easy enough to figure out, right? I’ll let you decide the answer to that for yourself.
µ
How would you do a range, Say I want everything between 11-01-2017 and 12-31-2017