Powershell:使用Foreach获取EC2Snapshot

Powershell:使用Foreach获取EC2Snapshot,powershell,amazon-ec2,foreach,Powershell,Amazon Ec2,Foreach,我得到了一个包含变量$volumeNames的脚本,其中包含一些卷IDvol-11111vol-2222… 现在我正试图使用另一个命令使用foreach处理所有这些ID,但它不起作用,我做错了什么 $AllSnapshots = [System.Collections.ArrayList]@() foreach ($volume in $volumeNames) { Get-EC2Snapshot -OwnerId $AWSAccount | Where-Object {$_.VolumeId

我得到了一个包含变量
$volumeNames
的脚本,其中包含一些卷ID
vol-11111vol-2222…

现在我正试图使用另一个命令使用
foreach
处理所有这些ID,但它不起作用,我做错了什么

$AllSnapshots = [System.Collections.ArrayList]@()
foreach ($volume in $volumeNames) {
Get-EC2Snapshot -OwnerId $AWSAccount | Where-Object {$_.VolumeId -eq $Volume}
    }
Write-Output "Total number of snapshots: $AllSnapshots.Count"
试试这个:

$AllSnapshots = foreach ($volume in $volumeNames) {
    Get-EC2Snapshot -OwnerId $AWSAccount | Where-Object {$_.VolumeId -eq $Volume}
}

Write-Output "Total number of snapshots: $($AllSnapshots.Count)"
这将导致
$allsnapshot
成为
Get-EC2Snapshot
返回的快照对象的集合

或者,您可以:

$AllSnapshots = [System.Collections.ArrayList]@()
foreach ($volume in $volumeNames) {
     $AllSnapshots += Get-EC2Snapshot -OwnerId $AWSAccount | Where-Object {$_.VolumeId -eq $Volume}
}

Write-Output "Total number of snapshots: $($AllSnapshots.Count)" 

请粘贴该变量所包含内容的精确格式。太好了!谢谢!