VirtualBox

Ticket #12687: ifcleanup-no-warranty.ps1

File ifcleanup-no-warranty.ps1, 6.7 KB (added by Frank Mehnert, 11 years ago)

Windows PowerShell script for clening up LUIDs on Windows 8 / 8.1 hosts

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy