fixing registry query logic

This commit is contained in:
Brian Warren 2025-04-06 22:03:46 -05:00
parent 5a604a6ef0
commit e1c5dfb76c
3 changed files with 114 additions and 100 deletions

View File

@ -3,28 +3,28 @@ param (
[string]
$airline = 'ua',
[Parameter(Mandatory=$true)]
[ValidateSet('query', 'export', 'import', 'delete')]
[string]
$action,
[Parameter(Mandatory=$true)]
[ValidateSet('gate', 'checkin')]
[string]
$comServerType,
$comType,
[Parameter(Mandatory=$true)]
[ValidateSet('export', 'import', 'delete', 'createRegFiles')]
[string]
$action,
[Parameter(Mandatory=$false)]
[ValidateSet('HKEY_LOCAL_MACHINE', 'HKEY_CURRENT_USER', 'HKLM', 'HKCU')]
[string]
$registryHive = 'HKEY_LOCAL_MACHINE',
$hive = 'HKEY_LOCAL_MACHINE',
[Parameter(Mandatory=$false)]
[string]
$comServerDir,
$comDir,
[Parameter(Mandatory=$false)]
[string]
$comServerFileName,
$comFileName,
[Parameter(Mandatory=$false)]
[switch]
@ -51,8 +51,9 @@ switch ($airline) {
exit 1
}
'ua' {
switch ($comServerType) {
switch ($comType) {
'checkin' {
$appName = 'uasrdev'
$clsid = '35578409-F9F5-43e8-A6BD-8B77199ED854'
$appId = '91D04D72-DCB6-11D3-957D-00409500D5AD'
$classLabel = "QWDev Class"
@ -65,6 +66,7 @@ switch ($airline) {
$versionIndependentProgId = 'QSRDevice.QSRDev';
}
'gate' {
$appName = 'uagrdev'
$clsid = '3F52625C-F66C-43F5-B538-CDF7331E0B7D'
$appId = 'C123476B-ABB6-4bcc-B557-2FDC0CDCD6A8'
$classLabel = "QSRDev Class"
@ -85,21 +87,21 @@ switch ($airline) {
}
switch ($action) {
'delete' {
'createRegFiles' {
}
'import' {
if (!$comServerDir) {
Write-Error "Missing COM server directory"
exit 1
}
if (!$comServerFileName) {
Write-Error "Missing COM server file name"
exit 1
}
'delete' {
}
'export' {
}
'query' {
'import' {
if (!$comDir) {
Write-Error "Missing COM server directory"
exit 1
}
if (!$comFileName) {
Write-Error "Missing COM server file name"
exit 1
}
}
default {
Write-Error "Unknown action: $action"
@ -107,58 +109,57 @@ switch ($action) {
}
}
switch ($registryHive) {
switch ($hive) {
'HKCU' {
$registryHiveShort = 'HKCU'
$registryHive = 'HKEY_CURRENT_USER'
$hiveShort = 'HKCU'
$hive = 'HKEY_CURRENT_USER'
}
'HKLM' {
$registryHiveShort = 'HKLM'
$registryHive = 'HKEY_LOCAL_MACHINE'
$hiveShort = 'HKLM'
$hive = 'HKEY_LOCAL_MACHINE'
}
'HKEY_CURRENT_USER' {
$registryHiveShort = 'HKCU'
$registryHive = 'HKEY_CURRENT_USER'
$hiveShort = 'HKCU'
$hive = 'HKEY_CURRENT_USER'
}
'HKEY_LOCAL_MACHINE' {
$registryHiveShort = 'HKLM'
$registryHive = 'HKEY_LOCAL_MACHINE'
$hiveShort = 'HKLM'
$hive = 'HKEY_LOCAL_MACHINE'
}
default {
Write-Error "Invalid registry hive: $registryHive"
Write-Error "Invalid registry hive: $hive"
exit 1
}
}
$comServerDir = $comServerDir.TrimEnd('\')
$comServerPath = "$comServerDir\$comServerFileName"
if (![System.IO.Path]::IsPathRooted($comServerPath)) {
Write-Error "Provided COM server path is not absolute: $comServerPath"
$comDir = $comDir.TrimEnd('\')
$comPath = "$comDir\$comFileName"
if (![System.IO.Path]::IsPathRooted($comPath)) {
Write-Error "Provided COM server path is not absolute: $comPath"
exit 1
}
$comServerNetworkPath = $comServerPath
if (!(Test-Path $comServerPath)) {
Write-Warning "COM server path does not exist: $comServerPath"
if (!(Test-Path $comPath)) {
Write-Warning "COM server path does not exist: $comPath"
}
# COM servers use the network path in some registry keys when self-registering, so attempt to discover it.
$comNetworkPath = $comPath
try {
$driveLetter = $comServerPath.Substring(0, 1)
$driveLetter = $comPath.Substring(0, 1)
$networkPathToDrive = (Get-PSDrive | Where-Object {$_.Name -eq $driveLetter}).DisplayRoot
if ($networkPathToDrive) {
$rootPathWithoutDrive = $comServerPath.Substring(2)
$comServerNetworkPath = "$networkPathToDrive$rootPathWithoutDrive"
$rootPathWithoutDrive = $comPath.Substring(2)
$comNetworkPath = "$networkPathToDrive$rootPathWithoutDrive"
}
} catch {
Write-Error "An error occurred trying to discover network path to COM server at $comServerPath"
Write-Error "An error occurred trying to discover network path to COM server at $comPath"
}
$progId = "$versionIndependentProgId.1";
$proxyStubClsid32 = '00020420-0000-0000-C000-000000000046'
$typeLibVersion = '1.0'
function Get-ClsidKeys {
function Get-ClsidPaths {
param(
[Parameter(Mandatory=$true)]
[string]
@ -183,7 +184,7 @@ function Get-ClsidKeys {
Values = @(
@{
Name = '(default)'
Value = $comServerPath
Value = $comPath
}
)
},
@ -216,7 +217,7 @@ function Get-ClsidKeys {
)
}
function Get-InterfaceKeys {
function Get-InterfacePaths {
param(
[Parameter(Mandatory=$true)]
[string]
@ -287,7 +288,7 @@ function Get-InterfaceKeys {
)
}
function Get-ProgIdKeys {
function Get-ProgIdPaths {
return @(
@{
Key = "\SOFTWARE\Classes\$progId"
@ -337,7 +338,7 @@ function Get-ProgIdKeys {
)
}
function Get-TypeLibKeys {
function Get-TypeLibPaths {
param(
[Parameter(Mandatory=$true)]
[string]
@ -366,7 +367,7 @@ function Get-TypeLibKeys {
Values = @(
@{
Name = '(default)'
Value = $comServerNetworkPath
Value = $comNetworkPath
}
)
},
@ -384,14 +385,14 @@ function Get-TypeLibKeys {
Values = @(
@{
Name = '(default)'
Value = "$comServerDir\"
Value = "$comDir\"
}
)
}
)
}
function Get-Keys {
function Get-Paths {
param(
[Parameter(Mandatory=$false)]
[switch]
@ -404,94 +405,79 @@ function Get-Keys {
$wow6432Node = '\Wow6432Node\'
}
$keys = New-Object System.Collections.Generic.List[System.Object]
$keys.AddRange((Get-ProgIdKeys))
$keys.AddRange((Get-ClsidKeys -architectureNode $wow6432Node))
$keys.AddRange((Get-InterfaceKeys -architectureNode $wow6432Node))
$keys.AddRange((Get-TypeLibKeys -architectureNode $wow6432Node))
$paths = New-Object System.Collections.Generic.List[System.Object]
$paths.AddRange((Get-ProgIdPaths))
$paths.AddRange((Get-ClsidPaths -architectureNode $wow6432Node))
$paths.AddRange((Get-InterfacePaths -architectureNode $wow6432Node))
$paths.AddRange((Get-TypeLibPaths -architectureNode $wow6432Node))
if ($wow6432Node -ne '\') {
if ($bothArchitectures) {
$keys.AddRange((Get-ClsidKeys -architectureNode $wow6432Node))
$paths.AddRange((Get-ClsidPaths -architectureNode $wow6432Node))
}
$keys.AddRange((Get-InterfaceKeys -architectureNode '\'))
$keys.AddRange((Get-TypeLibKeys -architectureNode '\'))
$paths.AddRange((Get-InterfacePaths -architectureNode '\'))
$paths.AddRange((Get-TypeLibPaths -architectureNode '\'))
}
return $keys
return $paths
}
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)"
$paths = Get-Paths -bothArchitectures
foreach ($path in $paths) {
$path = "$($hiveShort):\$($path.Key)"
try {
$foundKey = Get-Item -Path $path -ErrorAction Stop
}
catch {
Write-Debug "Failed to find $path"
Write-Warning "Failed to find $path"
continue
}
$pathShort = $foundKey.PSPath -replace '^.*::', ''
Write-Host
Write-Host "[$pathShort]"
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)"""
Write-Host "$($property.Name)=$property.Value"
}
}
}
switch ($action) {
'delete' {
$keys = Get-Keys -useRegFileFormat
foreach ($key in $keys) {
$paths = Get-Paths -bothArchitectures
foreach ($path in $paths) {
$path = "$($hiveShort):\$($path.Key)"
try {
Write-Debug "Deleting key: $($key.Key)"
# Remove-Item -Path $key.Key -Recurse -Force
# Write-Host "Deleted registry key: $($key.Key)"
$foundKey = Get-Item -Path $path -ErrorAction Stop
#Remove-Item -Path $path -Recurse -Force
Write-Information "Deleted registry key: $foundKey"
}
catch {
Write-Error "Failed to delete key: $($key.Key)"
Write-Error "Failed to find $path"
continue
}
}
}
'import' {
$keys = Get-Keys
foreach ($key in $keys) {
$paths = Get-Paths
foreach ($path in $paths) {
$path = "$($hiveShort):\$($path.Key)"
try {
Write-Debug "Importing key: $($key.Key)"
# $newKey = New-Item -Path $keyEl.Key -Force
# Write-Host "Created registry key: $($newKey.PSPath)"
$newKey = Get-Item -Path $path -ErrorAction Stop
Write-Information "Imported key: $newKey"
}
catch {
Write-Error "Failed to create key: $($key.Key)"
Write-Error "Failed to import key: $path"
continue
}
foreach ($value in $key.Values) {
try {
Write-Debug "Importing value: $($value.Name):$($value.Value)"
#New-ItemProperty -Path $key.Key -Name $value.Name -Value $value.Value -PropertyType 'String' -Force
#Write-Host "Created registry value: $($value.Name) in $($key.Key)"
#$newItem = New-ItemProperty -Path $key.Key -Name $value.Name -Value $value.Value -PropertyType 'String' -Force
#Write-Information "Imported value: $newItem"
Write-Information "Imported value: $($value.Name) in $($key.Key)"
}
catch {
Write-Error "Failed to create entry: $($value.Name) in $($key.Key)"
@ -501,11 +487,39 @@ switch ($action) {
}
}
'export' {
Read-Registry -useRegFileFormat
}
'query' {
Read-Registry
}
'createRegFiles' {
$outFileAdd = "$appName-add.reg"
$outFileDel = "$appName-del.reg"
$paths = Get-Paths
if (Test-Path $outFileAdd) {
try {
Remove-Item -Path $outFileAdd -ErrorAction Stop
}
catch {
Write-Error "Failed to delete existing file: $outFileAdd"
exit 1
}
}
Write-Output 'Windows Registry Editor Version 5.00' > $outFileAdd
Write-Output 'Windows Registry Editor Version 5.00' > $outFileDel
foreach ($pathItem in $paths) {
$path = "$($hive)$($pathItem.Key)"
Write-Output '' >> $outFileAdd
Write-Output "[$path]" >> $outFileAdd
Write-Output "[-$path]" >> $outFileDel
foreach ($valueItem in $pathItem.Values) {
if ($valueItem.Name -eq '(default)') {
$name = '@'
} else {
$name = """$($valueItem.Name)"""
}
$value = $valueItem.Value.Replace('\', '\\').Replace('"', '\"')
Write-Output "$name=""$value""" >> $outFileAdd
}
}
}
default {
Write-Error "Unknown action: $action"
exit 1

BIN
ua/registry/uasrdev-add.reg Normal file

Binary file not shown.

BIN
ua/registry/uasrdev-del.reg Normal file

Binary file not shown.