【问题标题】:Powershell: Using Foreach for Get-EC2SnapshotPowershell:使用 Foreach 获取 EC2Snapshot
【发布时间】:2018-02-21 16:15:40
【问题描述】:

我有一个脚本,其中包含变量 $volumeNames,其中包含一些卷 ID vol-11111 vol-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"

【问题讨论】:

  • 请粘贴该变量中包含的确切格式。

标签: powershell amazon-ec2 foreach


【解决方案1】:

试试这个:

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

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

这应该导致$AllSnapshotsGet-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)" 

【讨论】:

  • 太棒了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 2021-05-25
  • 2022-11-25
  • 2010-09-30
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多