PowerShell/Scripts/check-swap-space.ps1

46 lines
1.2 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-12-01 10:49:10 +01:00
Checks the swap space
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-01-29 12:47:46 +01:00
This PowerShell script checks the free swap space.
2021-10-16 16:50:10 +02:00
.PARAMETER MinLevel
2022-10-09 19:27:14 +02:00
Specifies the minimum level (10 GB by default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./check-swap-space
2022-10-09 19:27:14 +02:00
Swap space has 1826 GB left, 1856 GB total.
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-03-15 16:41:26 +01:00
#>
2022-10-09 19:27:14 +02:00
param([int]$MinLevel = 10) # minimum level in GB
2021-03-15 16:41:26 +01:00
try {
2021-03-20 15:15:57 +01:00
if ($IsLinux) {
$Result = $(free --mega | grep Swap:)
[int]$Total = $Result.subString(5,14)
[int]$Used = $Result.substring(20,13)
2022-10-09 19:21:41 +02:00
[int]$Free = $Result.substring(32,11)
2021-03-20 15:15:57 +01:00
} else {
$Items = get-wmiobject -class "Win32_PageFileUsage" -namespace "root\CIMV2" -computername localhost
foreach ($Item in $Items) {
[int]$Total = $Item.AllocatedBaseSize
[int]$Used = $Item.CurrentUsage
[int]$Free = ($Total - $Used)
}
}
2021-03-30 08:23:51 +02:00
if ($Total -eq "0") {
2022-10-09 19:27:14 +02:00
"⚠️ No swap space configured."
2021-12-01 10:49:10 +01:00
} elseif ($Free -lt $MinLevel) {
2022-10-09 19:27:14 +02:00
"⚠️ Swap space has only $Free GB left, $Total GB total!"
2021-12-01 10:49:10 +01:00
} else {
2022-10-09 19:27:14 +02:00
"✅ Swap space has $Free GB left, $Total GB total."
}
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-03-15 16:41:26 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-03-15 16:41:26 +01:00
exit 1
}