From 2359a1d1639743307b69d3274abb6da1ddce6c11 Mon Sep 17 00:00:00 2001 From: MyDrift Date: Wed, 13 Nov 2024 19:22:20 +0100 Subject: [PATCH] add a little bit of error handling - add error handling for Get-WinUtilToggleStatus --- functions/private/Get-WinUtilToggleStatus.ps1 | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/functions/private/Get-WinUtilToggleStatus.ps1 b/functions/private/Get-WinUtilToggleStatus.ps1 index da222472..3b0d329d 100644 --- a/functions/private/Get-WinUtilToggleStatus.ps1 +++ b/functions/private/Get-WinUtilToggleStatus.ps1 @@ -16,25 +16,34 @@ Function Get-WinUtilToggleStatus { $ToggleSwitchReg = $sync.configs.tweaks.$ToggleSwitch.registry - if (($ToggleSwitchReg.path -imatch "hku") -and !(Get-PSDrive -Name HKU -ErrorAction SilentlyContinue)) { - $null = (New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS) - if (Get-PSDrive -Name HKU -ErrorAction SilentlyContinue) { - Write-Debug "HKU drive created successfully" - } else { - Write-Debug "Failed to create HKU drive" + try { + if (($ToggleSwitchReg.path -imatch "hku") -and !(Get-PSDrive -Name HKU -ErrorAction SilentlyContinue)) { + $null = (New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS) + if (Get-PSDrive -Name HKU -ErrorAction SilentlyContinue) { + Write-Debug "HKU drive created successfully" + } else { + Write-Debug "Failed to create HKU drive" + } } + } catch { + Write-Error "An error occurred regarding the HKU Drive: $_" + return $false } if ($ToggleSwitchReg) { $count = 0 foreach ($regentry in $ToggleSwitchReg) { - $regstate = (Get-ItemProperty -path $regentry.Path).$($regentry.Name) - if ($regstate -eq $regentry.Value) { - $count += 1 - Write-Debug "$($regentry.Name) is true (state: $regstate, value: $($regentry.Value), original: $($regentry.OriginalValue))" - } else { - Write-Debug "$($regentry.Name) is false (state: $regstate, value: $($regentry.Value), original: $($regentry.OriginalValue))" + try { + $regstate = (Get-ItemProperty -path $regentry.Path).$($regentry.Name) + if ($regstate -eq $regentry.Value) { + $count += 1 + Write-Debug "$($regentry.Name) is true (state: $regstate, value: $($regentry.Value), original: $($regentry.OriginalValue))" + } else { + Write-Debug "$($regentry.Name) is false (state: $regstate, value: $($regentry.Value), original: $($regentry.OriginalValue))" + } + } catch { + Write-Error "An error occurred while accessing registry entry $($regentry.Path): $_" } }