mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-04 22:01:26 +01:00
3.0 KiB
3.0 KiB
The check-swap-space.ps1 Script
This PowerShell script queries the current status of the swap space and prints it.
Parameters
/home/markus/Repos/PowerShell/scripts/check-swap-space.ps1 [[-minLevel] <Int32>] [<CommonParameters>]
-minLevel <Int32>
Specifies the minimum level in MB (10 MB by default)
Required? false
Position? 1
Default value 10
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.
Example
PS> ./check-swap-space.ps1
✅ Swap space uses 1GB (21%) of 5GB
Notes
Author: Markus Fleschutz | License: CC0
Related Links
https://github.com/fleschutz/PowerShell
Script Content
<#
.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 1GB (21%) of 5GB
.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 with $(MB2String $total) is FULL !!!"
} elseif ($free -lt $minLevel) {
Write-Output "⚠️ Swap space has only $(MB2String $free) of $(MB2String $total) left!"
} elseif ($used -lt 3) {
Write-Output "✅ Swap space has $(MB2String $total) reserved"
} else {
[int64]$percent = ($used * 100) / $total
Write-Output "✅ Swap space uses $(MB2String $used) ($percent%) of $(MB2String $total)"
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:52)