forked from fleschutz/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-swap-space.ps1
More file actions
executable file
·64 lines (59 loc) · 1.92 KB
/
Copy pathcheck-swap-space.ps1
File metadata and controls
executable file
·64 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<#
.SYNOPSIS
Checks the swap space
.DESCRIPTION
This PowerShell script queries the current status of the swap space and prints it.
.PARAMETER minLevel
Specifies the minimum level in MB (10 MB by default)
.EXAMPLE
PS> ./check-swap-space.ps1
✅ Swap space uses 21% of 1GB - 1005MB free
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([int]$minLevel = 10)
function MB2String { param([int64]$bytes)
if ($bytes -lt 1024) { return "$($bytes)MB" }
$bytes /= 1024
if ($bytes -lt 1024) { return "$($bytes)GB" }
$bytes /= 1024
if ($bytes -lt 1024) { return "$($bytes)TB" }
$bytes /= 1024
if ($bytes -lt 1024) { return "$($bytes)PB" }
$bytes /= 1024
if ($bytes -lt 1024) { return "$($bytes)EB" }
}
try {
if ($IsLinux) {
$Result = $(free --mega | grep Swap:)
[int64]$total = $Result.subString(5,14)
[int64]$used = $Result.substring(20,13)
[int64]$free = $Result.substring(32,11)
} else {
$items = Get-WmiObject -class "Win32_PageFileUsage" -namespace "root\CIMV2" -computername localhost
[int64]$total = [int64]$used = 0
foreach ($item in $items) {
$total += $item.AllocatedBaseSize
$used += $item.CurrentUsage
}
[int64]$free = ($total - $used)
}
if ($total -eq 0) {
Write-Output "⚠️ No swap space configured"
} elseif ($free -eq 0) {
Write-Output "⚠️ Swap space is full ($(MB2String $total))"
} elseif ($free -lt $minLevel) {
Write-Output "⚠️ Swap space has only $(MB2String $free) of $(MB2String $total) left"
} elseif ($used -lt 3) {
Write-Output "✅ Swap space unused - $(MB2String $free) available"
} else {
[int64]$percent = ($free * 100) / $total
Write-Output "✅ Swap space has $(MB2String $free) of $(MB2String $total) left ($percent%)"
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}