现在有点困惑,想知道社区是否能给我一个快速的提振,以帮助我继续我正在工作的程序。
在我正在编写的程序中,我试图从一个数组中获取6个最新元素。我想把snapshot变量放在一个数组中,以便获得一个数组中的所有快照。下面是目前让我感到困惑的代码部分:
$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个最新变量之外的所有变量?
编辑:修复了以前没有注意到的小问题。
发布于 2012-07-07 00:12:52
您的代码有几个问题。我不确定这是否能修复您的脚本,但这些似乎是显而易见的问题,您应该首先修复它们。
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?
}发布于 2013-07-30 09:34:17
按创建的时间戳属性进行反向排序,然后在select对象中使用Skip来获取6个最新的
$snapshot = get-snapshot -VM "test"
$snapshot | sort created -descending | select -Skip 6 | Remove-Snapshot -Confirm:$falsehttps://stackoverflow.com/questions/11365089
复制相似问题