From 35cdf2cb96df659e2144c88c98f386428e0c9638 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Wed, 16 Aug 2023 08:04:14 +0200 Subject: [PATCH] Update check-drives.ps1 --- Scripts/check-drives.ps1 | 69 +++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/Scripts/check-drives.ps1 b/Scripts/check-drives.ps1 index 7d33076e..1be53043 100755 --- a/Scripts/check-drives.ps1 +++ b/Scripts/check-drives.ps1 @@ -2,60 +2,55 @@ .SYNOPSIS Checks the drive space .DESCRIPTION - This PowerShell script checks all drives for free space left. -.PARAMETER MinLevel + This PowerShell script queries the free space of all drives and prints it. +.PARAMETER minLevel Specifies the minimum warning level (10 GB by default) .EXAMPLE PS> ./check-drives.ps1 - ✅ Drive C: with 250GB at 10%, 225GB free + ✅ Drive C: at 49% of 1TB, 512GB free + ✅ Drive D: at 84% of 4TB, 641GB free .LINK https://github.com/fleschutz/PowerShell .NOTES Author: Markus Fleschutz | License: CC0 #> -param([int]$MinLevel = 10) # 10 GB minimum +param([int]$minLevel = 10) # 10 GB minimum -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" } +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 + return "$($bytes)PB" } try { Write-Progress "⏳ Querying drives..." - $Drives = Get-PSDrive -PSProvider FileSystem + $drives = Get-PSDrive -PSProvider FileSystem Write-Progress -completed "." - foreach($Drive in $Drives) { - $Details = (Get-PSDrive $Drive.Name) - if ($IsLinux) { $ID = $Drive.Name } else { $ID = $Drive.Name + ":" } - [int64]$Free = $Details.Free - [int64]$Used = $Details.Used - [int64]$Total = ($Used + $Free) + foreach($drive in $drives) { + $details = (Get-PSDrive $drive.Name) + if ($IsLinux) { $name = $drive.Name } else { $name = $drive.Name + ":" } + [int64]$free = $details.Free + [int64]$used = $details.Used + [int64]$total = ($used + $free) - if ($Total -eq 0) { - Write-Host "✅ Drive $ID is empty" - } elseif ($Free -eq 0) { - Write-Host "⚠️ Drive $ID with $(Bytes2String $Total) is 100% full" - } elseif ($Free -lt $MinLevel) { - Write-Host "⚠️ Drive $ID with $(Bytes2String $Total) is nearly full, $(Bytes2String $Free) free" + if ($total -eq 0) { + Write-Host "✅ Drive $name is empty" + } elseif ($free -eq 0) { + Write-Host "⚠️ Drive $name with $(Bytes2String $total) is 100% full" + } elseif ($free -lt $minLevel) { + Write-Host "⚠️ Drive $name with $(Bytes2String $total) is nearly full, $(Bytes2String $free) free" } else { - [int]$Percent = ($Used * 100) / $Total - 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" - } + [int]$percent = ($used * 100) / $total + Write-Host "✅ Drive $name at $percent% of $(Bytes2String $total), $(Bytes2String $free) free" } } exit 0 # success