fixing registry query logic

This commit is contained in:
Brian Warren 2025-04-06 18:43:55 -05:00
parent 050e08f497
commit 5a604a6ef0

View File

@ -4,7 +4,7 @@ param (
$airline = 'ua',
[Parameter(Mandatory=$true)]
[ValidateSet('query', 'delete', 'import')]
[ValidateSet('query', 'export', 'import', 'delete')]
[string]
$action,
@ -97,6 +97,8 @@ switch ($action) {
exit 1
}
}
'export' {
}
'query' {
}
default {
@ -107,14 +109,20 @@ switch ($action) {
switch ($registryHive) {
'HKCU' {
$registryHiveShort = 'HKCU'
$registryHive = 'HKEY_CURRENT_USER'
}
'HKLM' {
$registryHiveShort = 'HKLM'
$registryHive = 'HKEY_LOCAL_MACHINE'
}
'HKEY_CURRENT_USER' {
$registryHive = 'HKCU'
$registryHiveShort = 'HKCU'
$registryHive = 'HKEY_CURRENT_USER'
}
'HKEY_LOCAL_MACHINE' {
$registryHive = 'HKLM'
$registryHiveShort = 'HKLM'
$registryHive = 'HKEY_LOCAL_MACHINE'
}
default {
Write-Error "Invalid registry hive: $registryHive"
@ -158,7 +166,7 @@ function Get-ClsidKeys {
)
return @(
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}"
Key = "\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}"
Values = @(
@{
Name = '(default)'
@ -171,7 +179,7 @@ function Get-ClsidKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}\LocalServer32"
Key = "\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}\LocalServer32"
Values = @(
@{
Name = '(default)'
@ -180,7 +188,7 @@ function Get-ClsidKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}\ProgID"
Key = "\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}\ProgID"
Values = @(
@{
Name = '(default)'
@ -189,11 +197,11 @@ function Get-ClsidKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}\Programmable"
Key = "\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}\Programmable"
Values = @()
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}\TypeLib"
Key = "\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}\TypeLib"
Values = @(
@{
Name = '(default)'
@ -202,7 +210,7 @@ function Get-ClsidKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}\VersionIndependentProgID"
Key = "\SOFTWARE\Classes$($architectureNode)CLSID\{$clsid}\VersionIndependentProgID"
Values = @()
}
)
@ -216,7 +224,7 @@ function Get-InterfaceKeys {
)
return @(
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)Interface\{$interfaceId}"
Key = "\SOFTWARE\Classes$($architectureNode)Interface\{$interfaceId}"
Values = @(
@{
Name = '(default)'
@ -225,7 +233,7 @@ function Get-InterfaceKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)Interface\{$interfaceId}\ProxyStubClsid32"
Key = "\SOFTWARE\Classes$($architectureNode)Interface\{$interfaceId}\ProxyStubClsid32"
Values = @(
@{
Name = '(default)'
@ -234,7 +242,7 @@ function Get-InterfaceKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)Interface\{$interfaceId}\TypeLib"
Key = "\SOFTWARE\Classes$($architectureNode)Interface\{$interfaceId}\TypeLib"
Values = @(
@{
Name = '(default)'
@ -246,7 +254,7 @@ function Get-InterfaceKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)Interface\{$eventInterfaceId}"
Key = "\SOFTWARE\Classes$($architectureNode)Interface\{$eventInterfaceId}"
Values = @(
@{
Name = '(default)'
@ -255,7 +263,7 @@ function Get-InterfaceKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)Interface\{$eventInterfaceId}\ProxyStubClsid32"
Key = "\SOFTWARE\Classes$($architectureNode)Interface\{$eventInterfaceId}\ProxyStubClsid32"
Values = @(
@{
Name = '(default)'
@ -264,7 +272,7 @@ function Get-InterfaceKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)Interface\{$eventInterfaceId}\TypeLib"
Key = "\SOFTWARE\Classes$($architectureNode)Interface\{$eventInterfaceId}\TypeLib"
Values = @(
@{
Name = '(default)'
@ -282,7 +290,7 @@ function Get-InterfaceKeys {
function Get-ProgIdKeys {
return @(
@{
Key = "$($registryHive):\SOFTWARE\Classes\$progId"
Key = "\SOFTWARE\Classes\$progId"
Values = @(
@{
Name = '(default)'
@ -291,7 +299,7 @@ function Get-ProgIdKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes\$progId\CLSID"
Key = "\SOFTWARE\Classes\$progId\CLSID"
Values = @(
@{
Name = '(default)'
@ -300,7 +308,7 @@ function Get-ProgIdKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes\$progId\CurVer"
Key = "\SOFTWARE\Classes\$progId\CurVer"
Values = @(
@{
Name = '(default)'
@ -309,7 +317,7 @@ function Get-ProgIdKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes\$versionIndependentProgId"
Key = "\SOFTWARE\Classes\$versionIndependentProgId"
Values = @(
@{
Name = '(default)'
@ -318,7 +326,7 @@ function Get-ProgIdKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes\$versionIndependentProgId\CLSID"
Key = "\SOFTWARE\Classes\$versionIndependentProgId\CLSID"
Values = @(
@{
Name = '(default)'
@ -337,11 +345,11 @@ function Get-TypeLibKeys {
)
return @(
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}"
Key = "\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}"
Values = @()
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}\$typeLibVersion"
Key = "\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}\$typeLibVersion"
Values = @(
@{
Name = '(default)'
@ -350,11 +358,11 @@ function Get-TypeLibKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}\$typeLibVersion\0"
Key = "\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}\$typeLibVersion\0"
Values = @()
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}\$typeLibVersion\0\win32"
Key = "\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}\$typeLibVersion\0\win32"
Values = @(
@{
Name = '(default)'
@ -363,7 +371,7 @@ function Get-TypeLibKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}\$typeLibVersion\FLAGS"
Key = "\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}\$typeLibVersion\FLAGS"
Values = @(
@{
Name = '(default)'
@ -372,7 +380,7 @@ function Get-TypeLibKeys {
)
},
@{
Key = "$($registryHive):\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}\$typeLibVersion\HELPDIR"
Key = "\SOFTWARE\Classes$($architectureNode)TypeLib\{$typeLibId}\$typeLibVersion\HELPDIR"
Values = @(
@{
Name = '(default)'
@ -390,10 +398,10 @@ function Get-Keys {
$bothArchitectures = $false
)
if ($env:PROCESSOR_ARCHITECTURE -eq 'AMD64') {
$wow6432Node = '\Wow6432Node\'
} else {
if ($env:PROCESSOR_ARCHITECTURE -eq 'x86') {
$wow6432Node = '\'
} else {
$wow6432Node = '\Wow6432Node\'
}
$keys = New-Object System.Collections.Generic.List[System.Object]
@ -411,9 +419,49 @@ function Get-Keys {
return $keys
}
function Read-Registry {
param(
[Parameter(Mandatory=$false)]
[switch]
$useRegFileFormat = $false
)
$keys = Get-Keys -bothArchitectures
if ($useRegFileFormat) {
Write-Host 'Windows Registry Editor Version 5.00'
}
foreach ($key in $keys) {
$path = "$($registryHiveShort):\$($key.Key)"
try {
$foundKey = Get-Item -Path $path -ErrorAction Stop
}
catch {
Write-Debug "Failed to find $path"
continue
}
$pathShort = $foundKey.PSPath -replace '^.*::', ''
Write-Host
Write-Host "[$pathShort]"
$itemProperty = Get-ItemProperty -Path $foundKey.PSPath
if (!$itemProperty) {
continue
}
foreach ($property in $itemProperty.PSObject.Properties) {
if ($property.Name.StartsWith('PS')) {
continue
}
if ($property.Name -eq '(default)') {
$name = '@'
} else {
$name = """$($property.Name)"""
}
Write-Host "$($name)=""$($property.Value)"""
}
}
}
switch ($action) {
'delete' {
$keys = Get-Keys -bothArchitectures
$keys = Get-Keys -useRegFileFormat
foreach ($key in $keys) {
try {
Write-Debug "Deleting key: $($key.Key)"
@ -452,20 +500,14 @@ switch ($action) {
}
}
}
'export' {
Read-Registry -useRegFileFormat
}
'query' {
Read-Registry
}
default {
$keys = Get-Keys -bothArchitectures
foreach ($key in $keys) {
Write-Debug "[$($key.Key)]"
foreach ($valuePair in $key.Values) {
try {
$value = Get-ItemPropertyValue -Path $key.Key -Name $valuePair.Name -ErrorAction Stop
}
catch {
Write-Debug "Failed to find value for $($valuePair.Name)"
continue
}
Write-Host "$($valuePair.Name)=$($value)"
}
}
Write-Error "Unknown action: $action"
exit 1
}
}