diff --git a/scripts_wip/Win_Disk_Space_Check.ps1 b/scripts_wip/Win_Disk_Space_Check.ps1 index 99a08eb6..17242a1a 100644 --- a/scripts_wip/Win_Disk_Space_Check.ps1 +++ b/scripts_wip/Win_Disk_Space_Check.ps1 @@ -5,34 +5,44 @@ Long description Checks all FileSystem drives for an amount of space specified (amount is converted to Gigabytes). .EXAMPLE - Win_Disk_Space_Check -Size 10 + Confirm-DiskSpaceAvailable -Size 10 .EXAMPLE - Win_Disk_Space_Check -Size 10 -Percent + Confirm-DiskSpaceAvailable -Size 10 -Percent .NOTES Version: 1.0 Author: redanthrax Creation Date: 2022-04-05 + Updated: Owen Conti 2025-12-12 #> Param( [Parameter(Mandatory)] - [int]$Size, + [int]#The minimum amount of GB that should be available + $Size, [Parameter(Mandatory = $false)] - [switch]$Percent -) + [switch]#Switches the Size to be a percentage instead of GB + $Percent, -#Script Version -$sScriptVersion = "1.0" + [Parameter(Mandatory = $false)] + [switch]#Writes a message out even when the drive has more than the minimum available amount. In other words, logs *every* time. + $outputSuccess +) -function Win_Disk_Space_Check { +function Confirm-DiskSpaceAvailable { [CmdletBinding()] Param( [Parameter(Mandatory)] - [int]$Size, + [int]#The minimum amount of GB that should be available + $Size, + + [Parameter(Mandatory = $false)] + [switch]#Switches the Size to be a percentage instead of GB + $Percent, [Parameter(Mandatory = $false)] - [switch]$Percent + [switch]#Writes a message out even when the drive has more than the minimum available amount. In other words, logs *every* time. + $outputSuccess ) Begin {} @@ -40,25 +50,27 @@ function Win_Disk_Space_Check { Process { Try { $errors = 0 - $drives = Get-PSDrive | Where-Object { $_.Provider.Name -eq "FileSystem" -and $_.Used -gt 0 } + $drives = Get-PSDrive | Where-Object { $_.Provider.Name -eq "FileSystem" -and $_.Used -gt 0 -and $_.Name.ToLower() -ne "temp" } foreach ($drive in $drives) { + [string]$label = "GB" + [double]$available = 0 if ($Percent) { #Percent flag is set - #Calculate percent of space left on drive - $remainingPercent = [math]::Round($drive.Used / ($drive.Free + $drive.Used)) - $name = $drive.Name - if ($Size -gt $remainingPercent) { - Write-Output "$remainingPercent% space remaining on $name." - $errors += 1 - } + #Calculate percent of free space left on drive + $available = [math]::Round(($drive.Free / ($drive.Free + $drive.Used)) * 100,2) + $label = "%" } else { - $free = [math]::Round($drive.Free / 1Gb, 2) - $name = $drive.Name - if ($Size -gt $free) { - Write-Output "${free}GB of space on $name." - $errors += 1 - } + $available = [math]::Round($drive.Free / 1Gb, 2) + } + + If($outputSuccess){ + Write-Output "$available $label space remaining on $($drive.Name)." + } + + if ($Size -gt $available) { + Write-Output "ERROR: $($drive.Name) is below the threshold of $size $label ($available available)." + $errors += 1 } } } @@ -74,18 +86,19 @@ function Win_Disk_Space_Check { Exit 1 } - Write-Output "All disk space checked and clear." + Write-Output "All disks have been checked and have more than or equal to $size $label space available." Exit 0 } } -if (-not(Get-Command 'Win_Disk_Space_Check' -errorAction SilentlyContinue)) { +if (-not(Get-Command 'Confirm-DiskSpaceAvailable' -errorAction SilentlyContinue)) { . $MyInvocation.MyCommand.Path } $scriptArgs = @{ Size = $Size Percent = $Percent + outputSuccess = $outputSuccess } -Win_Disk_Space_Check @scriptArgs \ No newline at end of file +Confirm-DiskSpaceAvailable @scriptArgs \ No newline at end of file