比较前一个循环的值

时间:2017-08-13 15:34:48

标签: arrays powershell compare

我制作了这个脚本来监控服务器分区的可用空间。现在我尝试将最新的循环与前一个循环进行比较,将可用空间值存储到数组中,但我从来没有做过这样的事情。

我不知道如何使用数组 - 一个必须在循环开始时擦除,第二个必须存储以前的值,然后必须进行比较。

你能给我至少一个提示吗?

#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
}

2 个答案:

答案 0 :(得分:0)

复制以前的值

$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)值。

答案 1 :(得分:0)

如果要将当前值与先前运行的值进行比较,则需要将当前值存储在脚本末尾的某处(例如,在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 = Get-WmiObject Win32_LogicalDisk -Computer $hostname -Credential $cred |
              Select-Object DeviceID, Size, FreeSpace

您可以使用calculated properties添加派生信息(例如可用磁盘空间比率):

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

使用Compare-Object比较旧数据和新数据:

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