Reputation: 1
I'm trying to set up a Powershell script that copies volume snapshots in AWS from one region to another. I think the script below should work but I have a sneaking suspicion that my psobject $Snapshots is not getting properly populated with the matches from my source region. Kind of a PS noob, can anyone tell me how to troubleshoot the array fill or spot any obvious mistakes in my script? From the documentation, this should work:
# Adds snap-ins to the current powershell session for Powershell for Amazon Web Services.
if (-not (Get-Module AWSPowerShell -ErrorAction SilentlyContinue))
{Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1" > $null
}
Set-DefaultAWSRegion us-west-1
Creates the filter for qualifying snapshots
$Filter = (New-Object Amazon.EC2.Model.Filter).WithName("tag:SnapStatus").WithValue("SnapshotEBSEnabled")
# Loads the qualifying snapshots into an array of snapshots
$Snapshots = Get-EC2Snapshot -Region us-east-1 -Filter $Filter
# Loops through the snapshot objects and copies them from us-east-1 to us-west-1
foreach ($Snapshot in $Snapshots)
{$Snapshot | Where-Object {$_.Description -eq "SnapshotEBSEnabled"} | Copy-EC2Snapshot -SourceRegion us-east-1 -SourceSnapshotId $Snapshot.SnapshotId -Description "SnapshotEBSEnabled" -Region us-west-1
}
Upvotes: 0
Views: 477
Reputation: 7638
It looks like your ForEach block is a jumble. Try this:
$Snapshots |
Where { $_.Description -eq "SnapshotEBSEnabled" } |
ForEach-Object {
Copy-EC2Snapshot -SourceRegion us-east-1 -SourceSnapshotId $_.SnapshotId -Description "SnapshotEBSEnabled" -Region us-west-1
}
Upvotes: 1