From b4810644371f77cab04b6ab9e9b3bdac1192e067 Mon Sep 17 00:00:00 2001 From: titus Date: Thu, 5 Mar 2026 17:20:30 -0600 Subject: [PATCH] null-safe patches --- .../private/Invoke-WinUtilFeatureInstall.ps1 | 22 +++++++++++++--- functions/private/Invoke-WinUtilISOUSB.ps1 | 2 +- functions/private/Invoke-WinUtilScript.ps1 | 25 +++++++++++++++---- functions/private/Set-WinUtilDNS.ps1 | 9 ++++++- functions/private/Set-WinUtilRegistry.ps1 | 17 ++++++++++--- .../private/Set-WinUtilScheduledTask.ps1 | 15 ++++++++--- functions/private/Set-WinUtilService.ps1 | 9 ++++++- 7 files changed, 81 insertions(+), 18 deletions(-) diff --git a/functions/private/Invoke-WinUtilFeatureInstall.ps1 b/functions/private/Invoke-WinUtilFeatureInstall.ps1 index 335e85a5..b2cabce9 100644 --- a/functions/private/Invoke-WinUtilFeatureInstall.ps1 +++ b/functions/private/Invoke-WinUtilFeatureInstall.ps1 @@ -16,13 +16,20 @@ function Invoke-WinUtilFeatureInstall { Write-Host "Installing $feature" Enable-WindowsOptionalFeature -Online -FeatureName $feature -All -NoRestart } catch { - if ($CheckBox.Exception.Message -like "*requires elevation*") { + if ($_.Exception -and $_.Exception.Message -like "*requires elevation*") { Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?" Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" } } else { Write-Warning "Unable to Install $feature due to unhandled exception" - Write-Warning $CheckBox.Exception.StackTrace + if ($_.Exception) { + if (-not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } + if (-not [string]::IsNullOrWhiteSpace($_.Exception.StackTrace)) { + Write-Warning $_.Exception.StackTrace + } + } } } } @@ -35,13 +42,20 @@ function Invoke-WinUtilFeatureInstall { Write-Host "Running Script for $CheckBox" Invoke-Command $scriptblock -ErrorAction stop } catch { - if ($CheckBox.Exception.Message -like "*requires elevation*") { + if ($_.Exception -and $_.Exception.Message -like "*requires elevation*") { Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?" Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" } } else { Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" } Write-Warning "Unable to Install $feature due to unhandled exception" - Write-Warning $CheckBox.Exception.StackTrace + if ($_.Exception) { + if (-not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } + if (-not [string]::IsNullOrWhiteSpace($_.Exception.StackTrace)) { + Write-Warning $_.Exception.StackTrace + } + } } } } diff --git a/functions/private/Invoke-WinUtilISOUSB.ps1 b/functions/private/Invoke-WinUtilISOUSB.ps1 index e815de95..dccf72b3 100644 --- a/functions/private/Invoke-WinUtilISOUSB.ps1 +++ b/functions/private/Invoke-WinUtilISOUSB.ps1 @@ -265,4 +265,4 @@ function Invoke-WinUtilISOWriteUSB { }) | Out-Null $script.BeginInvoke() | Out-Null -} +} \ No newline at end of file diff --git a/functions/private/Invoke-WinUtilScript.ps1 b/functions/private/Invoke-WinUtilScript.ps1 index 05cef26a..4e4f9dac 100644 --- a/functions/private/Invoke-WinUtilScript.ps1 +++ b/functions/private/Invoke-WinUtilScript.ps1 @@ -25,20 +25,35 @@ function Invoke-WinUtilScript { Invoke-Command $scriptblock -ErrorAction Stop } catch [System.Management.Automation.CommandNotFoundException] { Write-Warning "The specified command was not found." - Write-Warning $PSItem.Exception.message + if ($_.Exception -and -not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } } catch [System.Management.Automation.RuntimeException] { Write-Warning "A runtime exception occurred." - Write-Warning $PSItem.Exception.message + if ($_.Exception -and -not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } } catch [System.Security.SecurityException] { Write-Warning "A security exception occurred." - Write-Warning $PSItem.Exception.message + if ($_.Exception -and -not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } } catch [System.UnauthorizedAccessException] { Write-Warning "Access denied. You do not have permission to perform this operation." - Write-Warning $PSItem.Exception.message + if ($_.Exception -and -not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } } catch { # Generic catch block to handle any other type of exception Write-Warning "Unable to run script for $name due to unhandled exception" - Write-Warning $psitem.Exception.StackTrace + if ($_.Exception) { + if (-not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } + if (-not [string]::IsNullOrWhiteSpace($_.Exception.StackTrace)) { + Write-Warning $_.Exception.StackTrace + } + } } } diff --git a/functions/private/Set-WinUtilDNS.ps1 b/functions/private/Set-WinUtilDNS.ps1 index 3aa4f157..58f5ac1e 100644 --- a/functions/private/Set-WinUtilDNS.ps1 +++ b/functions/private/Set-WinUtilDNS.ps1 @@ -28,6 +28,13 @@ function Set-WinUtilDNS { } } catch { Write-Warning "Unable to set DNS Provider due to an unhandled exception" - Write-Warning $psitem.Exception.StackTrace + if ($_.Exception) { + if (-not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } + if (-not [string]::IsNullOrWhiteSpace($_.Exception.StackTrace)) { + Write-Warning $_.Exception.StackTrace + } + } } } diff --git a/functions/private/Set-WinUtilRegistry.ps1 b/functions/private/Set-WinUtilRegistry.ps1 index e99aa669..2d12266d 100644 --- a/functions/private/Set-WinUtilRegistry.ps1 +++ b/functions/private/Set-WinUtilRegistry.ps1 @@ -46,11 +46,22 @@ function Set-WinUtilRegistry { } catch [System.Security.SecurityException] { Write-Warning "Unable to set $Path\$Name to $Value due to a Security Exception" } catch [System.Management.Automation.ItemNotFoundException] { - Write-Warning $psitem.Exception.ErrorRecord + Write-Warning $_.Exception.ErrorRecord } catch [System.UnauthorizedAccessException] { - Write-Warning $psitem.Exception.Message + if ($_.Exception -and -not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } else { + Write-Warning "Unauthorized access while setting $Path\$Name" + } } catch { Write-Warning "Unable to set $Name due to unhandled exception" - Write-Warning $psitem.Exception.StackTrace + if ($_.Exception) { + if (-not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } + if (-not [string]::IsNullOrWhiteSpace($_.Exception.StackTrace)) { + Write-Warning $_.Exception.StackTrace + } + } } } diff --git a/functions/private/Set-WinUtilScheduledTask.ps1 b/functions/private/Set-WinUtilScheduledTask.ps1 index e6ee2bef..45b21952 100644 --- a/functions/private/Set-WinUtilScheduledTask.ps1 +++ b/functions/private/Set-WinUtilScheduledTask.ps1 @@ -29,14 +29,23 @@ function Set-WinUtilScheduledTask { Enable-ScheduledTask -TaskName $Name -ErrorAction Stop } } catch [System.Exception] { - if($psitem.Exception.Message -like "*The system cannot find the file specified*") { + if ($_.Exception -and $_.Exception.Message -like "*The system cannot find the file specified*") { Write-Warning "Scheduled Task $name was not Found" } else { Write-Warning "Unable to set $Name due to unhandled exception" - Write-Warning $psitem.Exception.Message + if ($_.Exception -and -not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } } } catch { Write-Warning "Unable to run script for $name due to unhandled exception" - Write-Warning $psitem.Exception.StackTrace + if ($_.Exception) { + if (-not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } + if (-not [string]::IsNullOrWhiteSpace($_.Exception.StackTrace)) { + Write-Warning $_.Exception.StackTrace + } + } } } diff --git a/functions/private/Set-WinUtilService.ps1 b/functions/private/Set-WinUtilService.ps1 index 2070c4c0..f56cd4dc 100644 --- a/functions/private/Set-WinUtilService.ps1 +++ b/functions/private/Set-WinUtilService.ps1 @@ -34,7 +34,14 @@ Function Set-WinUtilService { Write-Warning "Service $Name was not found" } catch { Write-Warning "Unable to set $Name due to unhandled exception" - Write-Warning $_.Exception.Message + if ($_.Exception) { + if (-not [string]::IsNullOrWhiteSpace($_.Exception.Message)) { + Write-Warning $_.Exception.Message + } + if (-not [string]::IsNullOrWhiteSpace($_.Exception.StackTrace)) { + Write-Warning $_.Exception.StackTrace + } + } } }