现在有点难过,想知道社区是否能给我这么快的帮助,以帮助我继续我正在工作的计划。
在我正在进行的程序中,我正试图从阵列中获取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个以外的所有变量吗?
编辑:修复了以前没有注意到的小问题。
答案 0 :(得分: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?
}
答案 1 :(得分:0)
按创建的时间戳属性进行反向排序,然后使用select对象中的Skip
来获取最新的6个
$snapshot = get-snapshot -VM "test"
$snapshot | sort created -descending | select -Skip 6 | Remove-Snapshot -Confirm:$false