1

现在有点难过,想知道社区是否可以快速推动我,帮助我继续我正在工作的项目。

在我正在处理的程序中,我试图从数组中获取 6 个最新元素。我想将快照变量放在数组中,以便获取数组中的所有快照。这是目前让我感到困惑的代码部分:

$server = "test"
$date = get-date
$tempArray = @()
$snapshot = get-snapshot -VM "test"

foreach ($item in $snapshot){
    $tempArray += $item
}

$tempArray | sort
for ($i = 0; $i -le $tempArray.length-6; $i++){
    remove-item $tempArray[$i]
}

我是否实现了在我的数组中获取 $snapshot 变量的目标,并且我的 for 循环是否正确管理删除除了 6 个最新的变量之外的所有变量?

编辑:修复了以前没有注意到的小问题。

4

2 回答 2

0

你的代码有几个问题。我不确定这是否会修复您的脚本,但这些似乎是您应该首先解决的明显问题。

foreach ($item in $snapshot){
    $tempArray++ -> this should be $tempArray += $item, right? if you are adding $item to the tempArray
}

$tempArray | sort
for ($i = 0; $i -le $tempArray.length-6; $i++){
    remove-item $snapshot -> this should be remove-item $tempArray[$i], right?
}
于 2012-07-06T16:12:52.063 回答
0

按创建的时间戳属性进行反向排序,然后使用Skipin select 对象获取 6 个最新之后的所有内容

$snapshot = get-snapshot -VM "test"

$snapshot | sort created -descending | select -Skip 6 | Remove-Snapshot -Confirm:$false
于 2013-07-30T01:34:16.503 回答