From 076219c6a8447cb1eb2df45bbe30aede7f18c5ea Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 12 Dec 2025 13:50:37 +0000 Subject: [PATCH 1/5] Removed unused variable --- scripts_wip/Win_Disk_Space_Check.ps1 | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts_wip/Win_Disk_Space_Check.ps1 b/scripts_wip/Win_Disk_Space_Check.ps1 index 99a08eb6..798a481a 100644 --- a/scripts_wip/Win_Disk_Space_Check.ps1 +++ b/scripts_wip/Win_Disk_Space_Check.ps1 @@ -22,9 +22,6 @@ Param( [switch]$Percent ) -#Script Version -$sScriptVersion = "1.0" - function Win_Disk_Space_Check { [CmdletBinding()] Param( From d8468043a33dedbcbaddf25b7b596248e8b7e562 Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 12 Dec 2025 13:50:51 +0000 Subject: [PATCH 2/5] Renamed with approved verbs --- scripts_wip/Win_Disk_Space_Check.ps1 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts_wip/Win_Disk_Space_Check.ps1 b/scripts_wip/Win_Disk_Space_Check.ps1 index 798a481a..f2daece9 100644 --- a/scripts_wip/Win_Disk_Space_Check.ps1 +++ b/scripts_wip/Win_Disk_Space_Check.ps1 @@ -22,7 +22,7 @@ Param( [switch]$Percent ) -function Win_Disk_Space_Check { +function Confirm-DiskSpaceAvailable { [CmdletBinding()] Param( [Parameter(Mandatory)] @@ -39,11 +39,12 @@ function Win_Disk_Space_Check { $errors = 0 $drives = Get-PSDrive | Where-Object { $_.Provider.Name -eq "FileSystem" -and $_.Used -gt 0 } foreach ($drive in $drives) { + $name = $drive.Name 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 @@ -76,7 +77,7 @@ function Win_Disk_Space_Check { } } -if (-not(Get-Command 'Win_Disk_Space_Check' -errorAction SilentlyContinue)) { +if (-not(Get-Command 'Confirm-DiskSpaceAvailable' -errorAction SilentlyContinue)) { . $MyInvocation.MyCommand.Path } @@ -85,4 +86,4 @@ $scriptArgs = @{ Percent = $Percent } -Win_Disk_Space_Check @scriptArgs \ No newline at end of file +Confirm-DiskSpaceAvailable @scriptArgs \ No newline at end of file From b276122f8c2c4235aebe0bfe259361702da7f501 Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 12 Dec 2025 14:03:45 +0000 Subject: [PATCH 3/5] Refactored to condense logic, and correct issue with Percent reporting the percentage of FREE space available (was reporting the percentage of USED space) --- scripts_wip/Win_Disk_Space_Check.ps1 | 33 +++++++++++++--------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/scripts_wip/Win_Disk_Space_Check.ps1 b/scripts_wip/Win_Disk_Space_Check.ps1 index f2daece9..9b9d654e 100644 --- a/scripts_wip/Win_Disk_Space_Check.ps1 +++ b/scripts_wip/Win_Disk_Space_Check.ps1 @@ -5,9 +5,9 @@ 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 @@ -37,26 +37,23 @@ function Confirm-DiskSpaceAvailable { 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) { - $name = $drive.Name + [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)) - - 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 ($Size -gt $available) { + Write-Output "$available $label space remaining on $($drive.Name)." + $errors += 1 } } } @@ -72,7 +69,7 @@ function Confirm-DiskSpaceAvailable { 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 } } From 68fa0ac80b7899cd353d4d8a731bc504cf6d61a5 Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 12 Dec 2025 14:28:16 +0000 Subject: [PATCH 4/5] Updated output messages and added an option to log successes as well as errors --- scripts_wip/Win_Disk_Space_Check.ps1 | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/scripts_wip/Win_Disk_Space_Check.ps1 b/scripts_wip/Win_Disk_Space_Check.ps1 index 9b9d654e..89e7b8d4 100644 --- a/scripts_wip/Win_Disk_Space_Check.ps1 +++ b/scripts_wip/Win_Disk_Space_Check.ps1 @@ -12,6 +12,7 @@ Version: 1.0 Author: redanthrax Creation Date: 2022-04-05 + Updated: Owen Conti 2025-12-12 #> Param( @@ -26,10 +27,16 @@ function Confirm-DiskSpaceAvailable { [CmdletBinding()] 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, + + [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 ) Begin {} @@ -51,8 +58,12 @@ function Confirm-DiskSpaceAvailable { $available = [math]::Round($drive.Free / 1Gb, 2) } - if ($Size -gt $available) { + 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 } } From 5e45df7b0560ece11db68941323bf9036f29f214 Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 12 Dec 2025 14:36:16 +0000 Subject: [PATCH 5/5] Connected outputSuccess to the actual execution of the function. --- scripts_wip/Win_Disk_Space_Check.ps1 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/scripts_wip/Win_Disk_Space_Check.ps1 b/scripts_wip/Win_Disk_Space_Check.ps1 index 89e7b8d4..17242a1a 100644 --- a/scripts_wip/Win_Disk_Space_Check.ps1 +++ b/scripts_wip/Win_Disk_Space_Check.ps1 @@ -17,10 +17,16 @@ 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, + + [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 Confirm-DiskSpaceAvailable { @@ -92,6 +98,7 @@ if (-not(Get-Command 'Confirm-DiskSpaceAvailable' -errorAction SilentlyContinue) $scriptArgs = @{ Size = $Size Percent = $Percent + outputSuccess = $outputSuccess } Confirm-DiskSpaceAvailable @scriptArgs \ No newline at end of file