2012-07-06 89 views
1

現在稍微難倒了,並想知道社區是否可以快速提供幫助,以便繼續執行我正在工作的計劃。PowerCLI快照陣列

在我正在開發的程序中,我嘗試從數組中獲取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] 
} 

上午我實現我讓我的陣列內的$快照變量和目標是我的for循環正確地管理全部刪除,但6最新?

編輯:修正了以前沒有注意到的小問題。

回答

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? 
} 
0

反向排序創建的時間戳屬性然後使用Skip在選擇對象的6最新

$snapshot = get-snapshot -VM "test" 

$snapshot | sort created -descending | select -Skip 6 | Remove-Snapshot -Confirm:$false 
後得到的一切