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:

$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

aws-snapshots1a.jpg

You could also use multiple hash tables as shown in the following example:

Get-EC2Snapshot -Filter @(
    @{
        name='volume-id'
        value='myvolumeid'
    }
    @{
        name='status'
        value='completed'
    }
) |
Select-Object -First 1

aws-snapshots2a.jpg

Viewing the help for the Filter parameter shows what the valid names are so that part was easy enough to figure out:

help Get-EC2Snapshot -Parameter filter

aws-snapshots3a.jpg

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:

Get-EC2Snapshot -Filter @(
    @{
        name='volume-id'
        value='myvolumeid'
    }
    @{
        name='status'
        value='completed'
    }
    @{
        name='start-time'
        value='3/10/2015 1:02:20 AM'
    }
)

aws-snapshots4b.jpg

As it turns out, the value for StartTime has to be in the format shown in this example:

(Get-Date -Date '3/10/2015 1:02:20 AM').ToUniversalTime().ToString('yyyy-MM-ddThh:mm:ss.000Z')

aws-snapshots5a.jpg

Using that format for the StartTime value returned the one snapshot without issue:

Get-EC2Snapshot -Filter @(
    @{
        name='volume-id'
        value='myvolumeid'
    }
    @{
        name='status'
        value='completed'
    }
    @{
        name='start-time'
        value='2015-03-10T06:02:20.000Z'
    }
)

aws-snapshots6b.jpg

That was easy enough to figure out, right? I’ll let you decide the answer to that for yourself.

ยต