add a little bit of error handling

- add error handling for Get-WinUtilToggleStatus
This commit is contained in:
MyDrift 2024-11-13 19:22:20 +01:00
parent 34b8c7c3e4
commit 2359a1d163

View File

@ -16,6 +16,7 @@ Function Get-WinUtilToggleStatus {
$ToggleSwitchReg = $sync.configs.tweaks.$ToggleSwitch.registry $ToggleSwitchReg = $sync.configs.tweaks.$ToggleSwitch.registry
try {
if (($ToggleSwitchReg.path -imatch "hku") -and !(Get-PSDrive -Name HKU -ErrorAction SilentlyContinue)) { if (($ToggleSwitchReg.path -imatch "hku") -and !(Get-PSDrive -Name HKU -ErrorAction SilentlyContinue)) {
$null = (New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS) $null = (New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS)
if (Get-PSDrive -Name HKU -ErrorAction SilentlyContinue) { if (Get-PSDrive -Name HKU -ErrorAction SilentlyContinue) {
@ -24,11 +25,16 @@ Function Get-WinUtilToggleStatus {
Write-Debug "Failed to create HKU drive" Write-Debug "Failed to create HKU drive"
} }
} }
} catch {
Write-Error "An error occurred regarding the HKU Drive: $_"
return $false
}
if ($ToggleSwitchReg) { if ($ToggleSwitchReg) {
$count = 0 $count = 0
foreach ($regentry in $ToggleSwitchReg) { foreach ($regentry in $ToggleSwitchReg) {
try {
$regstate = (Get-ItemProperty -path $regentry.Path).$($regentry.Name) $regstate = (Get-ItemProperty -path $regentry.Path).$($regentry.Name)
if ($regstate -eq $regentry.Value) { if ($regstate -eq $regentry.Value) {
$count += 1 $count += 1
@ -36,6 +42,9 @@ Function Get-WinUtilToggleStatus {
} else { } else {
Write-Debug "$($regentry.Name) is false (state: $regstate, value: $($regentry.Value), original: $($regentry.OriginalValue))" 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): $_"
}
} }
if ($count -eq $ToggleSwitchReg.Count) { if ($count -eq $ToggleSwitchReg.Count) {