2021-12-03 10:12:56 +01:00
|
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
2022-10-25 15:15:43 +02:00
|
|
|
|
Checks the drive space
|
2021-12-03 10:12:56 +01:00
|
|
|
|
.DESCRIPTION
|
2022-10-25 15:15:43 +02:00
|
|
|
|
This PowerShell script checks all drives for free space left.
|
2022-10-09 19:40:51 +02:00
|
|
|
|
.PARAMETER MinLevel
|
|
|
|
|
Specifies the minimum warning level (10 GB by default)
|
2021-12-03 10:12:56 +01:00
|
|
|
|
.EXAMPLE
|
2023-08-06 18:13:42 +02:00
|
|
|
|
PS> ./check-drives.ps1
|
|
|
|
|
✅ Drive C: with 250GB at 10%, 225GB free
|
2021-12-03 10:12:56 +01:00
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
.NOTES
|
2022-06-09 16:30:28 +02:00
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-12-03 10:12:56 +01:00
|
|
|
|
#>
|
|
|
|
|
|
2022-10-09 19:40:51 +02:00
|
|
|
|
param([int]$MinLevel = 10) # 10 GB minimum
|
2022-10-09 12:34:06 +02:00
|
|
|
|
|
2022-11-06 22:06:55 +01:00
|
|
|
|
function Bytes2String { param([int64]$Bytes)
|
|
|
|
|
if ($Bytes -lt 1000) { return "$Bytes bytes" }
|
|
|
|
|
$Bytes /= 1000
|
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)KB" }
|
|
|
|
|
$Bytes /= 1000
|
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)MB" }
|
|
|
|
|
$Bytes /= 1000
|
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)GB" }
|
|
|
|
|
$Bytes /= 1000
|
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)TB" }
|
|
|
|
|
$Bytes /= 1000
|
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)PB" }
|
|
|
|
|
$Bytes /= 1000
|
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)EB" }
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 10:12:56 +01:00
|
|
|
|
try {
|
2023-01-23 13:39:02 +01:00
|
|
|
|
Write-Progress "⏳ Querying drives..."
|
2023-03-16 16:23:17 +01:00
|
|
|
|
$Drives = Get-PSDrive -PSProvider FileSystem
|
2023-08-05 15:58:50 +02:00
|
|
|
|
Write-Progress -completed "."
|
2021-12-03 10:12:56 +01:00
|
|
|
|
foreach($Drive in $Drives) {
|
2023-08-05 15:58:50 +02:00
|
|
|
|
$Details = (Get-PSDrive $Drive.Name)
|
2023-08-05 16:20:52 +02:00
|
|
|
|
if ($IsLinux) { $ID = $Drive.Name } else { $ID = $Drive.Name + ":" }
|
2022-11-06 22:06:55 +01:00
|
|
|
|
[int64]$Free = $Details.Free
|
|
|
|
|
[int64]$Used = $Details.Used
|
|
|
|
|
[int64]$Total = ($Used + $Free)
|
2021-12-03 10:12:56 +01:00
|
|
|
|
|
2022-10-25 15:15:43 +02:00
|
|
|
|
if ($Total -eq 0) {
|
2023-08-05 15:58:50 +02:00
|
|
|
|
Write-Host "✅ Drive $ID is empty"
|
2022-12-19 19:33:32 +01:00
|
|
|
|
} elseif ($Free -eq 0) {
|
2023-08-05 15:58:50 +02:00
|
|
|
|
Write-Host "⚠️ Drive $ID with $(Bytes2String $Total) is 100% full"
|
2021-12-06 19:21:15 +01:00
|
|
|
|
} elseif ($Free -lt $MinLevel) {
|
2023-08-06 11:57:43 +02:00
|
|
|
|
Write-Host "⚠️ Drive $ID with $(Bytes2String $Total) is nearly full, $(Bytes2String $Free) free"
|
2021-12-03 10:12:56 +01:00
|
|
|
|
} else {
|
2023-08-05 15:58:50 +02:00
|
|
|
|
[int]$Percent = ($Used * 100) / $Total
|
2023-08-06 18:13:42 +02:00
|
|
|
|
if ($Percent -gt 90) {
|
|
|
|
|
Write-Host "✅ Drive $ID with $(Bytes2String $Total) is $Percent% full, $(Bytes2String $Free) free"
|
|
|
|
|
} else {
|
|
|
|
|
Write-Host "✅ Drive $ID with $(Bytes2String $Total) at $Percent%, $(Bytes2String $Free) free"
|
|
|
|
|
}
|
2021-12-03 10:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
exit 0 # success
|
|
|
|
|
} catch {
|
2022-04-13 12:06:32 +02:00
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2021-12-03 10:12:56 +01:00
|
|
|
|
exit 1
|
2023-08-05 16:20:52 +02:00
|
|
|
|
}
|