improve handling if entry does not exist

- add DefaultState property
- add handler for DefaultState in Get-WinUtilToggleStatus
This commit is contained in:
MyDrift
2024-12-09 06:36:30 +01:00
parent 6b1a914e9e
commit a2c31008cf
2 changed files with 41 additions and 1 deletions

View File

@ -35,6 +35,9 @@ Function Get-WinUtilToggleStatus {
foreach ($regentry in $ToggleSwitchReg) {
try {
if (!(Test-Path $regentry.Path)) {
New-Item -Path $regentry.Path -Force | Out-Null
}
$regstate = (Get-ItemProperty -path $regentry.Path).$($regentry.Name)
if ($regstate -eq $regentry.Value) {
$count += 1
@ -42,8 +45,27 @@ Function Get-WinUtilToggleStatus {
} else {
Write-Debug "$($regentry.Name) is false (state: $regstate, value: $($regentry.Value), original: $($regentry.OriginalValue))"
}
if (!$regstate) {
write-host "missing $($regentry.Name)"
switch ($regentry.DefaultState) {
"true" {
write-host "true"
$regstate = $regentry.Value
$count += 1
}
"false" {
write-host "false"
$regstate = $regentry.OriginalValue
}
default {
write-host "default"
Write-Error "Entry for $($regentry.Name) does not exist and no DefaultState is defined."
$regstate = $regentry.OriginalValue
}
}
}
} catch {
Write-Error "An error occurred while accessing registry entry $($regentry.Path): $_"
Write-Error "An unexpected error occurred: $_"
}
}