PowerShell/docs/check-swap-space.md

111 lines
3.0 KiB
Markdown
Raw Normal View History

2024-11-08 12:38:20 +01:00
The *check-swap-space.ps1* Script
===========================
2021-11-08 21:36:42 +01:00
2023-10-19 08:12:00 +02:00
This PowerShell script queries the current status of the swap space and prints it.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/check-swap-space.ps1 [[-minLevel] <Int32>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2023-09-01 17:53:03 +02:00
-minLevel <Int32>
2024-08-15 09:51:46 +02:00
Specifies the minimum level in MB (10 MB by default)
2021-11-08 21:36:42 +01:00
Required? false
Position? 1
2022-11-17 19:46:02 +01:00
Default value 10
2021-11-08 21:36:42 +01:00
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2023-08-06 11:42:46 +02:00
PS> ./check-swap-space.ps1
2024-11-08 12:35:11 +01:00
✅ Swap space uses 1GB (21%) of 5GB
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2023-10-19 08:12:00 +02:00
Checks the swap space
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2023-10-19 08:12:00 +02:00
This PowerShell script queries the current status of the swap space and prints it.
2023-09-01 17:53:03 +02:00
.PARAMETER minLevel
2024-08-15 09:51:46 +02:00
Specifies the minimum level in MB (10 MB by default)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2023-08-06 11:42:46 +02:00
PS> ./check-swap-space.ps1
2024-11-08 12:35:11 +01:00
✅ Swap space uses 1GB (21%) of 5GB
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-09-01 17:53:03 +02:00
param([int]$minLevel = 10)
2022-11-17 20:02:26 +01:00
2024-03-27 17:36:59 +01:00
function MB2String { param([int64]$bytes)
2024-08-15 09:51:46 +02:00
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" }
2022-11-17 20:02:26 +01:00
}
try {
2024-03-27 17:36:59 +01:00
2022-11-17 20:02:26 +01:00
if ($IsLinux) {
$Result = $(free --mega | grep Swap:)
2024-03-27 17:36:59 +01:00
[int64]$total = $Result.subString(5,14)
[int64]$used = $Result.substring(20,13)
[int64]$free = $Result.substring(32,11)
2022-11-17 20:02:26 +01:00
} else {
2024-03-27 17:36:59 +01:00
$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)
2022-11-17 20:02:26 +01:00
}
2024-03-27 17:36:59 +01:00
if ($total -eq 0) {
2023-08-06 11:42:46 +02:00
Write-Output "⚠️ No swap space configured"
2024-03-27 17:36:59 +01:00
} elseif ($free -eq 0) {
2024-11-08 12:35:11 +01:00
Write-Output "⚠️ Swap space with $(MB2String $total) is FULL !!!"
2024-03-27 17:36:59 +01:00
} elseif ($free -lt $minLevel) {
2024-11-08 12:35:11 +01:00
Write-Output "⚠️ Swap space has only $(MB2String $free) of $(MB2String $total) left!"
2024-08-15 09:51:46 +02:00
} elseif ($used -lt 3) {
2024-11-08 12:35:11 +01:00
Write-Output "✅ Swap space has $(MB2String $total) reserved"
2022-11-17 20:02:26 +01:00
} else {
2024-03-27 17:36:59 +01:00
[int64]$percent = ($used * 100) / $total
2024-11-08 12:35:11 +01:00
Write-Output "✅ Swap space uses $(MB2String $used) ($percent%) of $(MB2String $total)"
2022-11-17 20:02:26 +01:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-08 12:40:31 +01:00
*(generated by convert-ps2md.ps1 as of 11/08/2024 12:40:16)*