1 | param([switch]$confirm)
|
---|
2 |
|
---|
3 | Function AskForConfirmation ($title_text, $message_text, $yes_text, $no_text)
|
---|
4 | {
|
---|
5 | if ($confirm) {
|
---|
6 | $title = $title_text
|
---|
7 | $message = $message_text
|
---|
8 |
|
---|
9 | $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", $yes_text
|
---|
10 |
|
---|
11 | $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", $no_text
|
---|
12 |
|
---|
13 | $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
|
---|
14 |
|
---|
15 | $result = $host.ui.PromptForChoice($title, $message, $options, 0)
|
---|
16 | } else {
|
---|
17 | $result = 0
|
---|
18 | }
|
---|
19 |
|
---|
20 | return $result
|
---|
21 | }
|
---|
22 |
|
---|
23 | Function DeleteUnmatchingKeys ($title_text, $reg_key)
|
---|
24 | {
|
---|
25 | $ghostcon = @(Get-ChildItem ($reg_key) | Where-Object { !$connections.ContainsKey($_.PSChildName) } )
|
---|
26 | if ($ghostcon.count -eq 0) {
|
---|
27 | Write-Host "`nNo ghost connections has been found -- nothing to do"
|
---|
28 | } else {
|
---|
29 | Write-Host "`nParameter keys for the following connections will be removed:"
|
---|
30 | Write-Host ($ghostcon | Out-String)
|
---|
31 |
|
---|
32 | $result = AskForConfirmation $title_text `
|
---|
33 | "Do you want to delete the keys listed above?" `
|
---|
34 | "Deletes all ghost connection keys from the registry." `
|
---|
35 | "No modifications to the registry will be made."
|
---|
36 |
|
---|
37 | switch ($result)
|
---|
38 | {
|
---|
39 | 0 {$ghostcon.GetEnumerator() | ForEach-Object { Remove-Item -Path $_ -Recurse }}
|
---|
40 | 1 {"Removal cancelled."}
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | Push-Location
|
---|
47 | cd "Registry::"
|
---|
48 | Write-Host "Retrieving valid connections:"
|
---|
49 | $iftypes = @{}
|
---|
50 | $connections = @{}
|
---|
51 | $ghostcon_names = @{}
|
---|
52 | Get-Item ".\HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0*" | `
|
---|
53 | ForEach-Object {
|
---|
54 | $prop = (Get-ItemProperty $_.PSPath)
|
---|
55 | $conn = $null
|
---|
56 | if (Test-Path ("HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" + $prop.NetCfgInstanceId + "\Connection")) {
|
---|
57 | $conn = (Get-ItemProperty ("HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" + $prop.NetCfgInstanceId + "\Connection"))
|
---|
58 | }
|
---|
59 | $iftype = $prop."*IfType"
|
---|
60 | if ($iftypes.ContainsKey($iftype)) {
|
---|
61 | $iftypes[$iftype] = $iftypes[$iftype] + [Math]::pow(2,$prop.NetLuidIndex)
|
---|
62 | } else {
|
---|
63 | $iftypes[$iftype] = [Math]::pow(2,$prop.NetLuidIndex)
|
---|
64 | }
|
---|
65 | if ($conn -ne $null) {
|
---|
66 | $connections[$prop.NetCfgInstanceId] = $conn.Name
|
---|
67 | Write-Host $prop.NetCfgInstanceId $conn.Name "|" $prop."*IfType" $prop.NetLuidIndex $prop.DriverDesc
|
---|
68 | } else {
|
---|
69 | Write-Host $prop.NetCfgInstanceId [MISSING] "|" $prop."*IfType" $prop.NetLuidIndex $prop.DriverDesc
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | # Someday we may want to process other types than Ethernet as well: $iftypes.GetEnumerator() | ForEach-Object {
|
---|
74 | if ($iftypes[6] -gt 9223372036854775808) {
|
---|
75 | Write-Host "Found more than 63 interfaces (mask=" $iftypes[6] ") -- bailing out"
|
---|
76 | exit
|
---|
77 | }
|
---|
78 | Write-Host "`nChecking if the used LUID index mask is correct:"
|
---|
79 | $correctmask = [BitConverter]::GetBytes([int64]($iftypes[6]))
|
---|
80 | $actualmask = (Get-ItemProperty -Path "HKLM\SYSTEM\CurrentControlSet\Services\NDIS\IfTypes\6" -Name "IfUsedNetLuidIndices").IfUsedNetLuidIndices
|
---|
81 | $needcorrection = $FALSE
|
---|
82 | $ai = 0
|
---|
83 | $lastnonzero = 0
|
---|
84 | for ($ci = 0; $ci -lt $correctmask.Length; $ci++) {
|
---|
85 | if ($ai -lt $actualmask.Length) {
|
---|
86 | $aval = $actualmask[$ai++]
|
---|
87 | } else {
|
---|
88 | $aval = 0
|
---|
89 | }
|
---|
90 | if ($correctmask[$ci] -ne 0) {
|
---|
91 | $lastnonzero = $ci
|
---|
92 | }
|
---|
93 | if ($correctmask[$ci] -eq $aval) {
|
---|
94 | Write-Host "DEBUG: " $correctmask[$ci].ToString("X2") " == " $aval.ToString("X2")
|
---|
95 | } else {
|
---|
96 | Write-Host "DEBUG: " $correctmask[$ci].ToString("X2") " != " $aval.ToString("X2")
|
---|
97 | $needcorrection = $TRUE
|
---|
98 | }
|
---|
99 | }
|
---|
100 | if ($ai -lt $actualmask.Length) {
|
---|
101 | for (; $ai -lt $actualmask.Length; $ai++) {
|
---|
102 | if ($actualmask[$ai] -eq 0) {
|
---|
103 | Write-Host "DEBUG: 0 == 0"
|
---|
104 | } else {
|
---|
105 | Write-Host "DEBUG: " $actualmask[$ai].ToString("X2") " != 0"
|
---|
106 | $needcorrection = $TRUE
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | if ($needcorrection) {
|
---|
111 | Write-Host "Current mask is " ($actualmask|foreach {$_.ToString("X2")}) ", while it should be" ($correctmask|foreach {$_.ToString("X2")})
|
---|
112 | if ($confirm) {
|
---|
113 | Set-ItemProperty -Path "HKLM\SYSTEM\CurrentControlSet\Services\NDIS\IfTypes\6" -Name "IfUsedNetLuidIndices" -Value $correctmask -Type Binary -Confirm
|
---|
114 | } else {
|
---|
115 | Set-ItemProperty -Path "HKLM\SYSTEM\CurrentControlSet\Services\NDIS\IfTypes\6" -Name "IfUsedNetLuidIndices" -Value $correctmask -Type Binary
|
---|
116 | }
|
---|
117 | } else {
|
---|
118 | Write-Host "The used LUID index mask is correct -- nothing to do"
|
---|
119 | }
|
---|
120 |
|
---|
121 | #Write-Host ($connections | Out-String)
|
---|
122 | $ghostcon = @(Get-ChildItem ("HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}") | Where-Object { !$connections.ContainsKey($_.PSChildName) -and $_.PSChildName -ne "Descriptions" } )
|
---|
123 | if ($ghostcon -eq $null) {
|
---|
124 | Write-Host "`nNo ghost connections has been found -- nothing to do"
|
---|
125 | } else {
|
---|
126 | Write-Host "`nThe following connections will be removed:"
|
---|
127 | #Write-Host ($ghostcon | Out-String)
|
---|
128 |
|
---|
129 | $ghostcon.GetEnumerator() | ForEach-Object {
|
---|
130 | $prop = (Get-ItemProperty "$_\Connection")
|
---|
131 | if ($prop.PnPInstanceId -eq $null) {
|
---|
132 | Write-Host "WARNING! PnPInstanceId does not exist for" $_.PSChildName
|
---|
133 | } elseif (!($prop.PnPInstanceId.ToString() -match "SUN_VBOXNETFLTMP")) {
|
---|
134 | Write-Host "WARNING! PnPInstanceId (" $prop.PnPInstanceId.ToString() ") does not match ROOT\SUN_VBOXNETFLTMP for" $_.PSChildName
|
---|
135 | }
|
---|
136 | if ($prop.Name -eq $null) {
|
---|
137 | Write-Host "WARNING! Name does not exist for" $_.PSChildName
|
---|
138 | } else {
|
---|
139 | $ghostcon_names.Add($_.PSChildName, $prop.Name)
|
---|
140 | Write-Host $_.PSChildName -nonewline
|
---|
141 | Write-Host " " -nonewline
|
---|
142 | Write-Host $prop.Name
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | $result = AskForConfirmation "Delete Registry Keys" `
|
---|
147 | "Do you want to delete the keys listed above?" `
|
---|
148 | "Deletes all ghost connection keys from the registry." `
|
---|
149 | "No modifications to the registry will be made."
|
---|
150 |
|
---|
151 | switch ($result)
|
---|
152 | {
|
---|
153 | 0 {$ghostcon.GetEnumerator() | ForEach-Object { Remove-Item -Path $_.PSPath -Recurse }}
|
---|
154 | 1 {"Removal cancelled."}
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | # Delete WFPLWFS parameter keys
|
---|
159 | DeleteUnmatchingKeys "Delete WFPLWFS Parameter Keys (Adapter subkey)" "HKLM\SYSTEM\CurrentControlSet\Services\WFPLWFS\Parameters\Adapters"
|
---|
160 | DeleteUnmatchingKeys "Delete WFPLWFS Parameter Keys (NdisAdapter subkey)" "HKLM\SYSTEM\CurrentControlSet\Services\WFPLWFS\Parameters\NdisAdapters"
|
---|
161 | # Delete Psched parameter keys
|
---|
162 | DeleteUnmatchingKeys "Delete Psched Parameter Keys (Adapter subkey)" "HKLM\SYSTEM\CurrentControlSet\Services\Psched\Parameters\Adapters"
|
---|
163 | DeleteUnmatchingKeys "Delete Psched Parameter Keys (NdisAdapter subkey)" "HKLM\SYSTEM\CurrentControlSet\Services\Psched\Parameters\NdisAdapters"
|
---|
164 |
|
---|
165 | # Clean up NSI entries
|
---|
166 | $nsi_obsolete = New-Object System.Collections.ArrayList
|
---|
167 | $nsi_path = "HKLM\SYSTEM\CurrentControlSet\Control\Nsi\{EB004A11-9B1A-11D4-9123-0050047759BC}\10"
|
---|
168 | $nsi = (Get-Item $nsi_path) | Select-Object -ExpandProperty property
|
---|
169 | $nsi | ForEach-Object {
|
---|
170 | $value = (Get-ItemProperty -Path $nsi_path -Name $_).$_
|
---|
171 | [byte[]]$guid_bytes = $value[1040..1055]
|
---|
172 | $guid = New-Object -TypeName System.Guid -ArgumentList (,$guid_bytes)
|
---|
173 | $guid_string = $guid.ToString("B").ToUpper()
|
---|
174 | $nsi_conn_name_last = 6 + $value[4] + $value[5]*256
|
---|
175 | $nsi_conn_name = [Text.Encoding]::Unicode.GetString($value[6..$nsi_conn_name_last])
|
---|
176 | $nsi_if_name_last = 522 + $value[520] + $value[521]*256
|
---|
177 | $nsi_if_name = [Text.Encoding]::Unicode.GetString($value[522..$nsi_if_name_last])
|
---|
178 | Write-Host $_ -nonewline
|
---|
179 | Write-Host " " -nonewline
|
---|
180 | Write-Host $guid_string -nonewline
|
---|
181 | Write-Host " " -nonewline
|
---|
182 | if ($connections.ContainsKey($guid_string)) {
|
---|
183 | Write-Host $nsi_if_name
|
---|
184 | } else {
|
---|
185 | [void] $nsi_obsolete.Add($_)
|
---|
186 | Write-Host "[OBSOLETE] " $nsi_if_name -foregroundcolor red
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | $result = AskForConfirmation "Delete NSI Entries" `
|
---|
191 | "Do you want to delete the entries marked in red above?" `
|
---|
192 | "Deletes all marked entries from the NSI registry key." `
|
---|
193 | "No modifications to the registry will be made."
|
---|
194 |
|
---|
195 | switch ($result)
|
---|
196 | {
|
---|
197 | 0 {$nsi_obsolete.GetEnumerator() | ForEach-Object { Remove-ItemProperty -Path $nsi_path -Name $_ }}
|
---|
198 | 1 {"Removal cancelled."}
|
---|
199 | }
|
---|
200 |
|
---|
201 | # Clean up uninstalled connections
|
---|
202 | if ( (Get-ChildItem "HKLM\SYSTEM\CurrentControlSet\Control\Network\Uninstalled" | Measure-Object).Count -gt 10 ) {
|
---|
203 | $result = AskForConfirmation "Delete Uninstalled Network Connection Registry Keys" `
|
---|
204 | "There are over 10 uninstalled network connections accumulated in the registry. Do you want to delete them?" `
|
---|
205 | "Deletes uninstalled connection keys from the registry." `
|
---|
206 | "No modifications to the registry will be made."
|
---|
207 |
|
---|
208 | switch ($result)
|
---|
209 | {
|
---|
210 | 0 {Remove-Item -Path "HKLM\SYSTEM\CurrentControlSet\Control\Network\Uninstalled\*" -Recurse}
|
---|
211 | 1 {"Removal cancelled."}
|
---|
212 | }
|
---|
213 | } else {
|
---|
214 | Write-Host "Less than 10 uninstalled connections -- no action yet required."
|
---|
215 | }
|
---|
216 |
|
---|
217 | Pop-Location
|
---|