如何在Powershell中检查psobject数组的内容?

时间:2013-10-08 17:18:55

标签: powershell amazon-web-services aws-powershell

我正在尝试设置一个Powershell脚本,将AWS中的卷快照从一个区域复制到另一个区域。我认为下面的脚本应该可以工作,但我怀疑我的psobject $ Snapshots没有正确填充来自我的源区域的匹配。 PS noob的种类,任何人都可以告诉我如何解决阵列填充或发现我脚本中的任何明显错误?从文档中,这应该工作:

# 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
    }

1 个答案:

答案 0 :(得分:1)

看起来你的ForEach区块是混乱的。试试这个:

$Snapshots | 
    Where { $_.Description -eq "SnapshotEBSEnabled" } |
    ForEach-Object { 
        Copy-EC2Snapshot -SourceRegion us-east-1 -SourceSnapshotId $_.SnapshotId -Description "SnapshotEBSEnabled" -Region us-west-1
    }