0

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
    }
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
fergie348
  • 1
  • 1

1 Answers1

1

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
    }
Eris
  • 7,378
  • 1
  • 30
  • 45
  • Thanks for the cleaned up for loop, but the snapshot copy is not working with either ForEach loop when run directly in the shell.. I don't know how to troubleshoot this in PowerShell - how can I examine the contents of the $Snapshots array to make sure it's being properly populated? – fergie348 Oct 09 '13 at 21:41
  • I normally load something like this into Powershell ISE and comment out everything using `<# (code lines) #>`. Then I move the `<#` down the file examining anything I want. You can also use ISE's built in debugger, or a third-party tool to debug. – Eris Oct 09 '13 at 22:03