mirror of
https://github.com/ChrisTitusTech/winutil
synced 2026-04-05 22:28:31 +00:00
null-safe patches
This commit is contained in:
@@ -16,13 +16,20 @@ function Invoke-WinUtilFeatureInstall {
|
|||||||
Write-Host "Installing $feature"
|
Write-Host "Installing $feature"
|
||||||
Enable-WindowsOptionalFeature -Online -FeatureName $feature -All -NoRestart
|
Enable-WindowsOptionalFeature -Online -FeatureName $feature -All -NoRestart
|
||||||
} catch {
|
} 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?"
|
Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?"
|
||||||
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" }
|
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" }
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
Write-Warning "Unable to Install $feature due to unhandled exception"
|
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"
|
Write-Host "Running Script for $CheckBox"
|
||||||
Invoke-Command $scriptblock -ErrorAction stop
|
Invoke-Command $scriptblock -ErrorAction stop
|
||||||
} catch {
|
} 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?"
|
Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?"
|
||||||
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" }
|
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" }
|
||||||
} else {
|
} else {
|
||||||
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" }
|
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" }
|
||||||
Write-Warning "Unable to Install $feature due to unhandled exception"
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -265,4 +265,4 @@ function Invoke-WinUtilISOWriteUSB {
|
|||||||
}) | Out-Null
|
}) | Out-Null
|
||||||
|
|
||||||
$script.BeginInvoke() | Out-Null
|
$script.BeginInvoke() | Out-Null
|
||||||
}
|
}
|
||||||
@@ -25,20 +25,35 @@ function Invoke-WinUtilScript {
|
|||||||
Invoke-Command $scriptblock -ErrorAction Stop
|
Invoke-Command $scriptblock -ErrorAction Stop
|
||||||
} catch [System.Management.Automation.CommandNotFoundException] {
|
} catch [System.Management.Automation.CommandNotFoundException] {
|
||||||
Write-Warning "The specified command was not found."
|
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] {
|
} catch [System.Management.Automation.RuntimeException] {
|
||||||
Write-Warning "A runtime exception occurred."
|
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] {
|
} catch [System.Security.SecurityException] {
|
||||||
Write-Warning "A security exception occurred."
|
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] {
|
} catch [System.UnauthorizedAccessException] {
|
||||||
Write-Warning "Access denied. You do not have permission to perform this operation."
|
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 {
|
} catch {
|
||||||
# Generic catch block to handle any other type of exception
|
# Generic catch block to handle any other type of exception
|
||||||
Write-Warning "Unable to run script for $name due to unhandled 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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,13 @@ function Set-WinUtilDNS {
|
|||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
Write-Warning "Unable to set DNS Provider due to an unhandled exception"
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,11 +46,22 @@ function Set-WinUtilRegistry {
|
|||||||
} catch [System.Security.SecurityException] {
|
} catch [System.Security.SecurityException] {
|
||||||
Write-Warning "Unable to set $Path\$Name to $Value due to a Security Exception"
|
Write-Warning "Unable to set $Path\$Name to $Value due to a Security Exception"
|
||||||
} catch [System.Management.Automation.ItemNotFoundException] {
|
} catch [System.Management.Automation.ItemNotFoundException] {
|
||||||
Write-Warning $psitem.Exception.ErrorRecord
|
Write-Warning $_.Exception.ErrorRecord
|
||||||
} catch [System.UnauthorizedAccessException] {
|
} 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 {
|
} catch {
|
||||||
Write-Warning "Unable to set $Name due to unhandled exception"
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,14 +29,23 @@ function Set-WinUtilScheduledTask {
|
|||||||
Enable-ScheduledTask -TaskName $Name -ErrorAction Stop
|
Enable-ScheduledTask -TaskName $Name -ErrorAction Stop
|
||||||
}
|
}
|
||||||
} catch [System.Exception] {
|
} 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"
|
Write-Warning "Scheduled Task $name was not Found"
|
||||||
} else {
|
} else {
|
||||||
Write-Warning "Unable to set $Name due to unhandled exception"
|
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 {
|
} catch {
|
||||||
Write-Warning "Unable to run script for $name due to unhandled 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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,14 @@ Function Set-WinUtilService {
|
|||||||
Write-Warning "Service $Name was not found"
|
Write-Warning "Service $Name was not found"
|
||||||
} catch {
|
} catch {
|
||||||
Write-Warning "Unable to set $Name due to unhandled exception"
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user