簡體   English   中英

如果 vm 的數據存儲的可用空間超過 %10,則 powercli 創建快照

[英]powercli create snapshot if vm's datastore has more than %10 free space

嗨,我在腳本上工作了好幾天。 如果 vm 的數據存儲需要空間,我想使用 vm snasphot。

我的腳本:

$myarray =@{}
$myarray = get-vm test | get-datastore | select-object @{N="DSFreespace"; E={[math]::Round(($_.FreeSpaceGB)/($_.CapacityGB)*100,2)}}
$Treshold = "{0:n2}" -f 10

foreach ($Treshold in $myarray) {
if ($myarray -ge $Treshold){new-snapshot -vm test -name test123} else {
Write-Host "You cannot take a snapshot. Datastore free spce is lower than 10%" }
}

當我在 shell 中運行腳本時,我也為同樣的事情編寫了另一個腳本,但沒有運氣。 當我使用“-ge”條件時,腳本總是拍攝虛擬機的快照,無論可用空間百分比(我嘗試了許多不同的數字,而不是原始閾值)

如果我使用“-gt”條件,無論可用空間百分比如何,腳本都不會拍攝快照。

我還嘗試了另一個腳本以獲得相同的細長、相同的結果。 此外,對於 -lt 和 -le 條件也一樣

$vm = get-vm test
$Treshold = "{0:n2}" -f 10
$DSFreespace = get-vm $vm| get-datastore | select-object @{N="DSFreespace"; E={[math]::Round(($_.FreeSpaceGB)/($_.CapacityGB)*100,2)}}
if($DSFreespace -ge $Treshold){new-snapshot -vm $vm -name test123} else {
Write-Host "You cannot take a snapshot. Datastore free space is lower than 10%" }'''

有什么問題,如何解決這個問題?

我通過添加解決了問題| Select-Object -ExpandProperty DSFreespace | Select-Object -ExpandProperty DSFreespace到 $dsfreespace 參數。

在該腳本由於輸出結果而失敗之前,屬性標簽為:

無法將“@{DSFreespace=16.12}”與“10.00”進行比較,因為對象的類型不同或對象“@{DSFreespace=16.12}”未實現“IComparable”。 在 line:5 char:5 + if (($dsfreespace -gt $treshold)) {new-snapshot -vm $vm -name test123} else { + ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException +fullyQualifiedErrorId : PSObjectCompareTo

如下更正代碼解決了所有問題。 它只需要結果值(在本例中為 16.12),而不是屬性標簽(DSFreespace)

 ForEach ($vm in (get-datastore -VM $vm) | ForEach {$_.VM}) {
$vm = get-vm test
$treshold = "{0:n2}" -f 10
$dsfreespace = get-datastore -VM $vm | select-object @{N="DSFreespace"; E={[math]::Round(($_.FreeSpaceGB)/($_.CapacityGB)*100,2)}} | Select-Object -ExpandProperty DSFreespace
if (($dsfreespace -gt $treshold)) {new-snapshot -vm $vm -name test123} else {
Write-Host "You cannot take a snapshot. Datastore free space is lower than 10%" }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM