RAM-Speicherbelegung ermitteln

Veröffentlicht:  24. Juni 2024

RAM Speicherverbrauch ermitteln und den Administrator beim Erreichen von festgelegten Grenzwerten warnen.

$WARNLEVEL = 65
$CRITLEVEL = 80

$ramusage = gwmi -Class win32_operatingsystem | Select-Object @{Name = "MemoryUsage"; Expression = {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}

if ( ($ramusage.MemoryUsage) -lt $WARNLEVEL ) {
	Write-Host "[OK] MEMORY usage is normal: "($ramusage.MemoryUsage)"%"
	exit 0
}
elseif (  ($ramusage.MemoryUsage) -gt $CRITLEVEL ) {
	Write-Host "[PROBLEM] MEMORY usage is high: "($ramusage.MemoryUsage)"%"
	exit 2
}
else {
	Write-Host "[WARNING] MEMORY usage is getting high: "($ramusage.MemoryUsage)"%"
	exit 1
}