AWS Storage Gateway User Guide
Understanding Volume Status and Transitions
2) Credentials and AWS Region stored in session using Initialize-AWSDefault.
For more info see, https://docs.amazonaws.cn/powershell/latest/userguide//specifying-
your-aws-credentials.html
.EXAMPLE
powershell.exe .\SG_DeleteSnapshots.ps1
#>
# Criteria to use to filter the results returned.
$daysBack = 18
$gatewayARN = "*** provide gateway ARN ***"
$viewOnly = $true;
#ListVolumes
$volumesResult = Get-SGVolume -GatewayARN $gatewayARN
$volumes = $volumesResult.VolumeInfos
Write-Output("`nVolume List")
foreach ($volumes in $volumesResult)
{ Write-Output("`nVolume Info:")
Write-Output("ARN: " + $volumes.VolumeARN)
write-Output("Type: " + $volumes.VolumeType)
}
Write-Output("`nWhich snapshots meet the criteria?")
foreach ($volume in $volumesResult)
{
$volumeARN = $volume.VolumeARN
$volumeId = ($volumeARN-split"/")[3].ToLower()
$filter = New-Object Amazon.EC2.Model.Filter
$filter.Name = "volume-id"
$filter.Value.Add($volumeId)
$snapshots = get-EC2Snapshot -Filter $filter
Write-Output("`nFor volume-id = " + $volumeId)
foreach ($s in $snapshots)
{
$d = ([DateTime]::Now).AddDays(-$daysBack)
$meetsCriteria = $false
if ([DateTime]::Compare($d, $s.StartTime) -gt 0)
{
$meetsCriteria = $true
}
$sb = $s.Snap ", " + $s.Sta ", meets criteria for delete? " +
$meetsCriteria
if (!$viewOnly -AND $meetsCriteria)
{
$resp = Remove-EC2Snapshot -SnapshotId $s.SnapshotId
#Can get RequestId from response for troubleshooting.
$sb = $sb + ", deleted? yes"
}
else {
$sb = $sb + ", deleted? no"
}
Write-Output($sb)
}
}
Understanding Volume Statuses and Transitions
Each volume has an associated status that tells you at a glance what the health of the volume is. Most
of the time, the status indicates that the volume is functioning normally and that no action is needed on
API Version 2013-06-30
171