簡體   English   中英

與上一個循環比較值

[英]Compare values with previous loop

我制作了這個腳本來監視服務器分區的可用空間。 現在,我試圖將最新的循環與上一個循環進行比較,以將可用空間值存儲到數組中,但是我從未做過這樣的事情。

我不知道如何精確地使用數組-一個必須在循環開始時刪除,第二個必須存儲先前的值,然后必須將它們進行比較。

您能否給我至少一個提示?

#play star-wars imperial march
function play-alarm {    
    Start-Job {
        [Console]::Beep(440,500)
    }
}
$cred = Get-Credential domain\username

$hostname = "server1"

#if free space lower, then play alarm
$low_level = "10"

#get drives letters
$partitions = Get-WmiObject Win32_LogicalDisk -Computername $hostname -Credential $cred | foreach DeviceID

#create arrays
$old_values = New-Object System.Collections.ArrayList
$new_values = New-Object System.Collections.ArrayList

#noob loop
$repeat = "2"

while ($i -lt $repeat) {
    Write-Host "        ***        " -ForegroundColor Yellow
    Write-Host "        ***        " -ForegroundColor Yellow
    Write-Host "        ***        " -ForegroundColor Yellow
    Write-Host "Free space at server:" -BackgroundColor Black

    #backup previouse values and clear array for new ones
    $old_values = $new_values
    $new_values.Clear()

    foreach ($partition in $partitions) {
        $device = "DeviceID='" + $partition + "'"

        $size = Get-WmiObject Win32_LogicalDisk -Credential $cred -ComputerName $hostname -Filter $device |
                ForEach-Object {$_.Size}
        $size = [Math]::Round($size/1GB)
        $free = Get-WmiObject Win32_LogicalDisk -Credential $cred -ComputerName $hostname -Filter $device |
                ForEach-Object {$_.FreeSpace}
        $free = [Math]::Round($free/1GB)
        Write-Host $free
        #add value rounded to GB
        $new_values.Add($free)

        #if device is CD-ROM, the size or space is zero - this cause error when calculating percentage of free space
        if ($size -eq "0") {
            Write-Host "disk " $partition "is CD-ROM" -ForegroundColor Yellow
        } else {
            $perc = ($free/$size)*100
            $perc = [Math]::Round($perc, 3)

            if ($perc -le $low_level) {
                Write-Host "Not enough free space at partition " $partition "!!!" $perc "%" -BackgroundColor Red #| play-alarm
                Start-Sleep -s 15
            } else {
                Write-Host "disk " $partition "is OK - " $perc "%" -ForegroundColor Green
            }
        }
    }

    if ($old_values -eq $new_values) {
        Write-Host "no change..."
    } else {
        Write-Host "Attention - change!!!" -BackgroundColor Red
    }

    $time = $((Get-Date).ToString())
    Write-Host "Loop finished..." $time -BackgroundColor Black
    Write-Host "        ***        " -ForegroundColor Yellow
    Write-Host "        ***        " -ForegroundColor Yellow
    Write-Host "        ***        " -ForegroundColor Yellow
    Start-Sleep -s 300
}

復制以前的值

$old_values = $new_values
$new_values.Clear()

問題在於$ old_values現在與$ new_values指向同一數組。 采用

$old_values = $new_values.Clone()

創建副本。

相比

您不想使用-eq比較容器對象的內容。 您可以遍歷數組並比較每個元素

for ($index=0;$index -lt $old_values.count;$index+=1) {
    if ($old_values[$index] -ne $new_values[$index]) {
        write-host "Changed!"
        break
    }
}

或者您可以使用Compare-Object

    if (Compare-Object $old_values $new_values) {
        write-host "No Change!"
    } else {
        write-host "Changed!"
    }

Compare-Object返回差異列表。 如果您比較的內容相同,那么它將返回一個空值(布爾值為false)。

如果要將當前值與先前運行的值進行比較,則需要將當前值存儲在腳本末尾的某個位置(例如,在CSV文件中),並在開始時讀取該文件(如果存在)。腳本。

$csv = 'C:\path\to\data.csv'

if (Test-Path -LiteralPath $csv) {
    $old_values = Import-Csv $csv
} else {
    # initialize $old_values as an empty array if the file doesn't exist (yet)
    $old_values = @()
}

...

$new_values | Export-Csv $csv -NoType

通過選擇WMI數據的相關屬性,將$new_values構建$new_values自定義對象的列表。 除非將其輸出給用戶,否則請避免格式化磁盤空間數據。

$new_values = Get-WmiObject Win32_LogicalDisk -Computer $hostname -Credential $cred |
              Select-Object DeviceID, Size, FreeSpace

您可以使用計算出的屬性來添加派生信息(例如可用磁盤空間比率):

... | Select-Object DeviceID, Size, FreeSpace, @{n='Ratio';e={$_.FreeSpace/$_.Size}}

使用Compare-Object比較舊數據和新數據:

Compare-Object -ReferenceObject $old_values -DifferenceObject $new_values -Property DeviceID, Size, FreeSpace

暫無
暫無

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

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