|
1 | 1 | param ( |
2 | | - [string]$DriverName = "ODBC Driver for BigQuery", |
| 2 | + [string]$BaseDriverName = "ODBC Driver for BigQuery", |
3 | 3 | [string[]]$SystemDsnRoots = @( |
4 | 4 | "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI", |
5 | 5 | "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\ODBC\ODBC.INI" |
6 | 6 | ), |
7 | 7 | [string]$Platform = "" |
8 | 8 | ) |
9 | 9 |
|
10 | | -$shouldDeleteUserDSNs = $true |
11 | | -if ($Platform -eq "x64" -and (Test-Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\ODBC\ODBCINST.INI\$DriverName")) { |
12 | | - $shouldDeleteUserDSNs = $false |
13 | | -} elseif (($Platform -eq "x86" -or $Platform -eq "x32" -or $Platform -eq "") -and (Test-Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\$DriverName")) { |
14 | | - $shouldDeleteUserDSNs = $false |
| 10 | +if ($Platform -eq "x64") { |
| 11 | + $DriverName = "$BaseDriverName (64-bit)" |
| 12 | +} elseif ($Platform -eq "x86" -or $Platform -eq "x32" -or $Platform -eq "") { |
| 13 | + $DriverName = "$BaseDriverName (32-bit)" |
| 14 | +} else { |
| 15 | + $DriverName = $BaseDriverName |
15 | 16 | } |
16 | 17 |
|
17 | | -if ($shouldDeleteUserDSNs) { |
18 | | - # Get all user SIDs under HKEY_USERS, excluding system _Classes keys |
19 | | - $all_sids = Get-ChildItem "Registry::HKEY_USERS" | Where-Object { |
20 | | - $_.Name -notmatch "_Classes$" |
21 | | - } |
| 18 | +# Get all user SIDs under HKEY_USERS, excluding system _Classes keys |
| 19 | +$all_sids = Get-ChildItem "Registry::HKEY_USERS" | Where-Object { |
| 20 | + $_.Name -notmatch "_Classes$" |
| 21 | +} |
| 22 | + |
| 23 | +foreach ($sid_entry in $all_sids) { |
| 24 | + $sid = ($sid_entry.Name -split '\\')[-1] # Extract SID |
| 25 | + |
| 26 | + $dsn_path_root = "Registry::HKEY_USERS\$sid\Software\ODBC\ODBC.INI" |
| 27 | + $odbc_sources_path = "$dsn_path_root\ODBC Data Sources" |
| 28 | + |
| 29 | + if (Test-Path $odbc_sources_path) { |
| 30 | + try { |
| 31 | + $dsns = Get-ItemProperty -Path $odbc_sources_path |
| 32 | + foreach ($property in $dsns.PSObject.Properties) { |
| 33 | + $name = $property.Name |
| 34 | + if ($name -notmatch "^PS.*") { |
| 35 | + $driver = $property.Value |
22 | 36 |
|
23 | | - foreach ($sid_entry in $all_sids) { |
24 | | - $sid = ($sid_entry.Name -split '\\')[-1] # Extract SID |
25 | | - |
26 | | - $dsn_path_root = "Registry::HKEY_USERS\$sid\Software\ODBC\ODBC.INI" |
27 | | - $odbc_sources_path = "$dsn_path_root\ODBC Data Sources" |
28 | | - |
29 | | - if (Test-Path $odbc_sources_path) { |
30 | | - try { |
31 | | - $dsns = Get-ItemProperty -Path $odbc_sources_path |
32 | | - foreach ($property in $dsns.PSObject.Properties) { |
33 | | - $name = $property.Name |
34 | | - if ($name -notmatch "^PS.*") { |
35 | | - $driver = $property.Value |
36 | | - |
37 | | - if ($driver -eq $DriverName) { |
38 | | - # Delete DSN-specific registry entries |
39 | | - Remove-Item -Path "$dsn_path_root\$name" -Recurse -Force -ErrorAction SilentlyContinue |
40 | | - Remove-ItemProperty -Path $odbc_sources_path -Name $name -ErrorAction SilentlyContinue |
41 | | - } |
| 37 | + if ($driver -eq $DriverName) { |
| 38 | + # Delete DSN-specific registry entries |
| 39 | + Remove-Item -Path "$dsn_path_root\$name" -Recurse -Force -ErrorAction SilentlyContinue |
| 40 | + Remove-ItemProperty -Path $odbc_sources_path -Name $name -ErrorAction SilentlyContinue |
42 | 41 | } |
43 | 42 | } |
44 | | - } catch { |
45 | 43 | } |
| 44 | + } catch { |
46 | 45 | } |
47 | 46 | } |
| 47 | +} |
48 | 48 |
|
49 | | - function Load_UserHive { |
50 | | - param ( |
51 | | - [string]$Sid, |
52 | | - [string]$UserProfilePath |
53 | | - ) |
54 | | - $ntuser_dat = Join-Path $UserProfilePath "NTUSER.DAT" |
55 | | - if (Test-Path $ntuser_dat) { |
56 | | - reg load "HKU\$Sid" "$ntuser_dat" | Out-Null |
57 | | - } |
| 49 | +function Load_UserHive { |
| 50 | + param ( |
| 51 | + [string]$Sid, |
| 52 | + [string]$UserProfilePath |
| 53 | + ) |
| 54 | + $ntuser_dat = Join-Path $UserProfilePath "NTUSER.DAT" |
| 55 | + if (Test-Path $ntuser_dat) { |
| 56 | + reg load "HKU\$Sid" "$ntuser_dat" | Out-Null |
58 | 57 | } |
| 58 | +} |
59 | 59 |
|
60 | | - function Unload_UserHive { |
61 | | - param ( |
62 | | - [string]$Sid |
63 | | - ) |
64 | | - reg unload "HKU\$Sid" | Out-Null |
65 | | - } |
| 60 | +function Unload_UserHive { |
| 61 | + param ( |
| 62 | + [string]$Sid |
| 63 | + ) |
| 64 | + reg unload "HKU\$Sid" | Out-Null |
| 65 | +} |
66 | 66 |
|
67 | | - # User DSNs (HKU) |
68 | | - $hku_path = "Registry::HKEY_USERS" |
69 | | - $sids = Get-ChildItem -Path $hku_path | Where-Object { $_.Name -notmatch "_Classes$" } |
| 67 | +# User DSNs (HKU) |
| 68 | +$hku_path = "Registry::HKEY_USERS" |
| 69 | +$sids = Get-ChildItem -Path $hku_path | Where-Object { $_.Name -notmatch "_Classes$" } |
70 | 70 |
|
71 | | - # Supplement with user profiles |
72 | | - $user_profiles = Get-ChildItem "C:\Users" | Where-Object { |
73 | | - Test-Path "$($_.FullName)\NTUSER.DAT" |
74 | | - } |
| 71 | +# Supplement with user profiles |
| 72 | +$user_profiles = Get-ChildItem "C:\Users" | Where-Object { |
| 73 | + Test-Path "$($_.FullName)\NTUSER.DAT" |
| 74 | +} |
75 | 75 |
|
76 | | - foreach ($user in $user_profiles) { |
77 | | - $user_path = $user.FullName |
78 | | - $user_sid = (Get-CimInstance -Class Win32_UserAccount | Where-Object { $_.Name -eq $user.Name }).SID |
79 | | - if ($user_sid -and -not ($sids.Name -match [regex]::Escape($user_sid))) { |
80 | | - if (Load_UserHive -Sid $user_sid -UserProfilePath $user_path) { |
81 | | - $sids += Get-Item "Registry::HKEY_USERS\$user_sid" |
82 | | - } |
| 76 | +foreach ($user in $user_profiles) { |
| 77 | + $user_path = $user.FullName |
| 78 | + $user_sid = (Get-CimInstance -Class Win32_UserAccount | Where-Object { $_.Name -eq $user.Name }).SID |
| 79 | + if ($user_sid -and -not ($sids.Name -match [regex]::Escape($user_sid))) { |
| 80 | + if (Load_UserHive -Sid $user_sid -UserProfilePath $user_path) { |
| 81 | + $sids += Get-Item "Registry::HKEY_USERS\$user_sid" |
83 | 82 | } |
84 | 83 | } |
| 84 | +} |
85 | 85 |
|
86 | | - foreach ($sid in $sids) { |
87 | | - $sid_name = $sid.PSChildName |
88 | | - $user_dsn_root = "Registry::HKEY_USERS\$sid_name\Software\ODBC\ODBC.INI" |
89 | | - $odbc_sources_path = "$user_dsn_root\ODBC Data Sources" |
| 86 | +foreach ($sid in $sids) { |
| 87 | + $sid_name = $sid.PSChildName |
| 88 | + $user_dsn_root = "Registry::HKEY_USERS\$sid_name\Software\ODBC\ODBC.INI" |
| 89 | + $odbc_sources_path = "$user_dsn_root\ODBC Data Sources" |
90 | 90 |
|
91 | | - if (Test-Path $odbc_sources_path) { |
92 | | - $sources_key = Get-Item -Path $odbc_sources_path |
93 | | - $sources = $sources_key.GetValueNames() |
94 | | - } else { |
95 | | - continue |
96 | | - } |
| 91 | + if (Test-Path $odbc_sources_path) { |
| 92 | + $sources_key = Get-Item -Path $odbc_sources_path |
| 93 | + $sources = $sources_key.GetValueNames() |
| 94 | + } else { |
| 95 | + continue |
| 96 | + } |
97 | 97 |
|
98 | | - foreach ($dsn in $sources) { |
99 | | - $driver = $sources_key.GetValue($dsn) |
100 | | - if ($driver -eq $DriverName) { |
101 | | - Remove-Item -Path "$user_dsn_root\$dsn" -Recurse -Force -ErrorAction SilentlyContinue |
102 | | - Remove-ItemProperty -Path $odbc_sources_path -Name $dsn -ErrorAction SilentlyContinue |
103 | | - } |
| 98 | + foreach ($dsn in $sources) { |
| 99 | + $driver = $sources_key.GetValue($dsn) |
| 100 | + if ($driver -eq $DriverName) { |
| 101 | + Remove-Item -Path "$user_dsn_root\$dsn" -Recurse -Force -ErrorAction SilentlyContinue |
| 102 | + Remove-ItemProperty -Path $odbc_sources_path -Name $dsn -ErrorAction SilentlyContinue |
104 | 103 | } |
| 104 | + } |
105 | 105 |
|
106 | | - if ($user_profiles.Name -contains $sid_name) { |
107 | | - Unload_UserHive -Sid $sid_name |
108 | | - } |
| 106 | + if ($user_profiles.Name -contains $sid_name) { |
| 107 | + Unload_UserHive -Sid $sid_name |
109 | 108 | } |
110 | 109 | } |
111 | 110 |
|
|
0 commit comments