2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncparam([switch]$confirm)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncFunction AskForConfirmation ($title_text, $message_text, $yes_text, $no_text)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($confirm) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $title = $title_text
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $message = $message_text
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", $yes_text
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", $no_text
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $result = $host.ui.PromptForChoice($title, $message, $options, 0)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $result = 0
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync return $result
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncFunction DeleteUnmatchingKeys ($title_text, $reg_key)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $ghostcon = @(Get-ChildItem ($reg_key) | Where-Object { !$connections.ContainsKey($_.PSChildName) } )
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "`nNo ghost connections has been found -- nothing to do"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "`nParameter keys for the following connections will be removed:"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host ($ghostcon | Out-String)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $result = AskForConfirmation $title_text `
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync "Do you want to delete the keys listed above?" `
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync "Deletes all ghost connection keys from the registry." `
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync "No modifications to the registry will be made."
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync switch ($result)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync 0 {$ghostcon.GetEnumerator() | ForEach-Object { Remove-Item -Path $_ -Recurse }}
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync 1 {"Removal cancelled."}
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncPush-Location
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsynccd "Registry::"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncWrite-Host "Retrieving valid connections:"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$iftypes = @{}
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$connections = @{}
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$ghostcon_names = @{}
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncGet-Item ".\HKLM\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0*" | `
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync ForEach-Object {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $prop = (Get-ItemProperty $_.PSPath)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $conn = $null
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if (Test-Path ("HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" + $prop.NetCfgInstanceId + "\Connection")) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $conn = (Get-ItemProperty ("HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" + $prop.NetCfgInstanceId + "\Connection"))
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $iftype = $prop."*IfType"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($iftypes.ContainsKey($iftype)) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $iftypes[$iftype] = $iftypes[$iftype] + [Math]::pow(2,$prop.NetLuidIndex)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $iftypes[$iftype] = [Math]::pow(2,$prop.NetLuidIndex)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($conn -ne $null) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host $prop.NetCfgInstanceId $conn.Name "|" $prop."*IfType" $prop.NetLuidIndex $prop.DriverDesc
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host $prop.NetCfgInstanceId [MISSING] "|" $prop."*IfType" $prop.NetLuidIndex $prop.DriverDesc
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync# Someday we may want to process other types than Ethernet as well: $iftypes.GetEnumerator() | ForEach-Object {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncif ($iftypes[6] -gt 9223372036854775808) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "Found more than 63 interfaces (mask=" $iftypes[6] ") -- bailing out"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncWrite-Host "`nChecking if the used LUID index mask is correct:"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$correctmask = [BitConverter]::GetBytes([int64]($iftypes[6]))
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$actualmask = (Get-ItemProperty -Path "HKLM\SYSTEM\CurrentControlSet\Services\NDIS\IfTypes\6" -Name "IfUsedNetLuidIndices").IfUsedNetLuidIndices
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$needcorrection = $FALSE
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$lastnonzero = 0
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncfor ($ci = 0; $ci -lt $correctmask.Length; $ci++) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($ai -lt $actualmask.Length) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $aval = $actualmask[$ai++]
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($correctmask[$ci] -ne 0) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $lastnonzero = $ci
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($correctmask[$ci] -eq $aval) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "DEBUG: " $correctmask[$ci].ToString("X2") " == " $aval.ToString("X2")
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "DEBUG: " $correctmask[$ci].ToString("X2") " != " $aval.ToString("X2")
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $needcorrection = $TRUE
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync for (; $ai -lt $actualmask.Length; $ai++) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($actualmask[$ai] -eq 0) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "DEBUG: 0 == 0"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "DEBUG: " $actualmask[$ai].ToString("X2") " != 0"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $needcorrection = $TRUE
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncif ($needcorrection) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "Current mask is " ($actualmask|foreach {$_.ToString("X2")}) ", while it should be" ($correctmask|foreach {$_.ToString("X2")})
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($confirm) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Set-ItemProperty -Path "HKLM\SYSTEM\CurrentControlSet\Services\NDIS\IfTypes\6" -Name "IfUsedNetLuidIndices" -Value $correctmask -Type Binary -Confirm
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Set-ItemProperty -Path "HKLM\SYSTEM\CurrentControlSet\Services\NDIS\IfTypes\6" -Name "IfUsedNetLuidIndices" -Value $correctmask -Type Binary
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "The used LUID index mask is correct -- nothing to do"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync#Write-Host ($connections | Out-String)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$ghostcon = @(Get-ChildItem ("HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}") | Where-Object { !$connections.ContainsKey($_.PSChildName) -and $_.PSChildName -ne "Descriptions" } )
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncif ($ghostcon -eq $null) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "`nNo ghost connections has been found -- nothing to do"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "`nThe following connections will be removed:"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync #Write-Host ($ghostcon | Out-String)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $ghostcon.GetEnumerator() | ForEach-Object {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $prop = (Get-ItemProperty "$_\Connection")
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($prop.PnPInstanceId -eq $null) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "WARNING! PnPInstanceId does not exist for" $_.PSChildName
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync } elseif (!($prop.PnPInstanceId.ToString() -match "SUN_VBOXNETFLTMP")) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "WARNING! PnPInstanceId (" $prop.PnPInstanceId.ToString() ") does not match ROOT\SUN_VBOXNETFLTMP for" $_.PSChildName
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($prop.Name -eq $null) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "WARNING! Name does not exist for" $_.PSChildName
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host $_.PSChildName -nonewline
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host " " -nonewline
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $result = AskForConfirmation "Delete Registry Keys" `
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync "Do you want to delete the keys listed above?" `
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync "Deletes all ghost connection keys from the registry." `
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync "No modifications to the registry will be made."
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync switch ($result)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync 0 {$ghostcon.GetEnumerator() | ForEach-Object { Remove-Item -Path $_.PSPath -Recurse }}
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync 1 {"Removal cancelled."}
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync# Delete WFPLWFS parameter keys
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncDeleteUnmatchingKeys "Delete WFPLWFS Parameter Keys (Adapter subkey)" "HKLM\SYSTEM\CurrentControlSet\Services\WFPLWFS\Parameters\Adapters"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncDeleteUnmatchingKeys "Delete WFPLWFS Parameter Keys (NdisAdapter subkey)" "HKLM\SYSTEM\CurrentControlSet\Services\WFPLWFS\Parameters\NdisAdapters"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync# Delete Psched parameter keys
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncDeleteUnmatchingKeys "Delete Psched Parameter Keys (Adapter subkey)" "HKLM\SYSTEM\CurrentControlSet\Services\Psched\Parameters\Adapters"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncDeleteUnmatchingKeys "Delete Psched Parameter Keys (NdisAdapter subkey)" "HKLM\SYSTEM\CurrentControlSet\Services\Psched\Parameters\NdisAdapters"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync# Clean up NSI entries
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$nsi_obsolete = New-Object System.Collections.ArrayList
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$nsi_path = "HKLM\SYSTEM\CurrentControlSet\Control\Nsi\{EB004A11-9B1A-11D4-9123-0050047759BC}\10"
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$nsi = (Get-Item $nsi_path) | Select-Object -ExpandProperty property
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$nsi | ForEach-Object {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $value = (Get-ItemProperty -Path $nsi_path -Name $_).$_
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync [byte[]]$guid_bytes = $value[1040..1055]
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $guid = New-Object -TypeName System.Guid -ArgumentList (,$guid_bytes)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $guid_string = $guid.ToString("B").ToUpper()
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $nsi_conn_name_last = 6 + $value[4] + $value[5]*256
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $nsi_conn_name = [Text.Encoding]::Unicode.GetString($value[6..$nsi_conn_name_last])
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $nsi_if_name_last = 522 + $value[520] + $value[521]*256
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync $nsi_if_name = [Text.Encoding]::Unicode.GetString($value[522..$nsi_if_name_last])
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host $_ -nonewline
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host " " -nonewline
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host $guid_string -nonewline
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host " " -nonewline
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync if ($connections.ContainsKey($guid_string)) {
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host $nsi_if_name
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync Write-Host "[OBSOLETE] " $nsi_if_name -foregroundcolor red
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync$result = AskForConfirmation "Delete NSI Entries" `
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync "Do you want to delete the entries marked in red above?" `
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync "Deletes all marked entries from the NSI registry key." `
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync "No modifications to the registry will be made."
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsyncswitch ($result)
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync 0 {$nsi_obsolete.GetEnumerator() | ForEach-Object { Remove-ItemProperty -Path $nsi_path -Name $_ }}
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync 1 {"Removal cancelled."}
45739bec18b4266fea3d2adfc9dbb52f50ffabfdvboxsync# Clean up uninstalled connections
45739bec18b4266fea3d2adfc9dbb52f50ffabfdvboxsyncif ( (Get-ChildItem "HKLM\SYSTEM\CurrentControlSet\Control\Network\Uninstalled" | Measure-Object).Count -gt 10 ) {
45739bec18b4266fea3d2adfc9dbb52f50ffabfdvboxsync $result = AskForConfirmation "Delete Uninstalled Network Connection Registry Keys" `
45739bec18b4266fea3d2adfc9dbb52f50ffabfdvboxsync "There are over 10 uninstalled network connections accumulated in the registry. Do you want to delete them?" `
45739bec18b4266fea3d2adfc9dbb52f50ffabfdvboxsync "Deletes uninstalled connection keys from the registry." `
45739bec18b4266fea3d2adfc9dbb52f50ffabfdvboxsync "No modifications to the registry will be made."
45739bec18b4266fea3d2adfc9dbb52f50ffabfdvboxsync switch ($result)
45739bec18b4266fea3d2adfc9dbb52f50ffabfdvboxsync 0 {Remove-Item -Path "HKLM\SYSTEM\CurrentControlSet\Control\Network\Uninstalled\*" -Recurse}
45739bec18b4266fea3d2adfc9dbb52f50ffabfdvboxsync 1 {"Removal cancelled."}
45739bec18b4266fea3d2adfc9dbb52f50ffabfdvboxsync Write-Host "Less than 10 uninstalled connections -- no action yet required."
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncPop-Location