一键修复有网但是浏览器无法打开网址问题脚本

image.png
# 浏览器网络连接问题诊断和修复脚本
# 需要以管理员权限运行

Write-Host "浏览器网络连接问题诊断修复工具" -ForegroundColor Green
Write-Host "======================================" -ForegroundColor Yellow
Write-Host ""

# 检查管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Host "错误: 请以管理员权限运行此脚本!" -ForegroundColor Red
    Read-Host "按回车键退出"
    exit
}

try {
    # 1. 检查网络连接状态
    Write-Host "1. 检查网络连接状态..." -ForegroundColor Cyan
    $networkAdapters = Get-NetAdapter | Where-Object {$_.Status -eq "Up"}
    if ($networkAdapters) {
        Write-Host "   ✓ 网络适配器正常:" -ForegroundColor Green
        $networkAdapters | ForEach-Object { Write-Host "     - $($_.Name)" -ForegroundColor Gray }
    } else {
        Write-Host "   ✗ 未发现活动的网络适配器" -ForegroundColor Red
        Write-Host "     请检查网络连接是否正常" -ForegroundColor Yellow
    }
    
    # 2. 检查DNS设置
    Write-Host "2. 检查DNS设置..." -ForegroundColor Cyan
    $dnsServers = Get-DnsClientServerAddress | Where-Object {$_.ServerAddresses -ne $null}
    if ($dnsServers) {
        Write-Host "   ✓ DNS服务器配置正常" -ForegroundColor Green
        $dnsServers | ForEach-Object { 
            if ($_.ServerAddresses) {
                Write-Host "     - $($_.InterfaceAlias): $($_.ServerAddresses -join ', ')" -ForegroundColor Gray 
            }
        }
    } else {
        Write-Host "   ⚠ DNS服务器未配置" -ForegroundColor Yellow
    }
    
    # 3. 检查和修复代理设置
    Write-Host "3. 检查代理设置..." -ForegroundColor Cyan
    $regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
    $proxySettings = Get-ItemProperty -Path $regPath
    
    Write-Host "   当前代理状态:" -ForegroundColor White
    Write-Host "     代理启用: $($proxySettings.ProxyEnable)" -ForegroundColor Gray
    Write-Host "     代理服务器: $($proxySettings.ProxyServer)" -ForegroundColor Gray
    Write-Host "     自动配置: $($proxySettings.AutoConfigURL)" -ForegroundColor Gray
    
    if ($proxySettings.ProxyEnable -eq 1) {
        Write-Host "   发现代理已启用,正在禁用..." -ForegroundColor Yellow
        Set-ItemProperty -Path $regPath -Name "ProxyEnable" -Value 0
        if ($proxySettings.ProxyServer) {
            Remove-ItemProperty -Path $regPath -Name "ProxyServer" -ErrorAction SilentlyContinue
        }
        Write-Host "   ✓ 代理已禁用" -ForegroundColor Green
    } else {
        Write-Host "   ✓ 代理未启用" -ForegroundColor Green
    }
    
    # 4. 检查Hosts文件
    Write-Host "4. 检查Hosts文件..." -ForegroundColor Cyan
    $hostsPath = "$env:windir\System32\drivers\etc\hosts"
    if (Test-Path $hostsPath) {
        $hostsContent = Get-Content $hostsPath | Where-Object {$_ -notmatch "^#" -and $_ -notmatch "^\s*$"}
        $defaultEntries = @("127.0.0.1       localhost", "::1             localhost")
        $customEntries = $hostsContent | Where-Object { $defaultEntries -notcontains $_.Trim() }
        
        if ($customEntries) {
            Write-Host "   ⚠ 发现自定义Hosts条目:" -ForegroundColor Yellow
            $customEntries | ForEach-Object { Write-Host "     $_" -ForegroundColor Gray }
            
            $backupHosts = Read-Host "   是否备份并清理Hosts文件?(Y/N)"
            if ($backupHosts -eq 'Y' -or $backupHosts -eq 'y') {
                Copy-Item $hostsPath "$hostsPath.backup.$(Get-Date -Format 'yyyyMMdd_HHmmss')"
                $defaultContent = @(
                    "# Copyright (c) 1993-2009 Microsoft Corp.",
                    "#",
                    "# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.",
                    "#",
                    "127.0.0.1       localhost",
                    "::1             localhost"
                )
                $defaultContent | Set-Content $hostsPath
                Write-Host "   ✓ Hosts文件已重置,原文件已备份" -ForegroundColor Green
            }
        } else {
            Write-Host "   ✓ Hosts文件正常" -ForegroundColor Green
        }
    }
    
    # 5. 清除DNS缓存
    Write-Host "5. 清除DNS缓存..." -ForegroundColor Cyan
    try {
        Clear-DnsClientCache
        Write-Host "   ✓ DNS缓存已清除" -ForegroundColor Green
    } catch {
        Write-Host "   ✗ DNS缓存清除失败: $($_.Exception.Message)" -ForegroundColor Red
    }
    
    # 6. 重置网络组件
    Write-Host "6. 重置网络组件..." -ForegroundColor Cyan
    
    # 重置Winsock
    Write-Host "   重置Winsock..." -ForegroundColor White
    $winsockResult = netsh winsock reset 2>&1
    if ($LASTEXITCODE -eq 0) {
        Write-Host "   ✓ Winsock重置成功" -ForegroundColor Green
    } else {
        Write-Host "   ⚠ Winsock重置可能失败" -ForegroundColor Yellow
    }
    
    # 重置TCP/IP
    Write-Host "   重置TCP/IP..." -ForegroundColor White
    $tcpipResult = netsh int ip reset 2>&1
    if ($LASTEXITCODE -eq 0) {
        Write-Host "   ✓ TCP/IP重置成功" -ForegroundColor Green
    } else {
        Write-Host "   ⚠ TCP/IP重置可能失败" -ForegroundColor Yellow
    }
    
    # 7. 检查防火墙设置
    Write-Host "7. 检查Windows防火墙..." -ForegroundColor Cyan
    $firewallProfiles = Get-NetFirewallProfile
    $blockingRules = 0
    foreach ($profile in $firewallProfiles) {
        if ($profile.Enabled -and $profile.DefaultInboundAction -eq "Block") {
            $blockingRules++
        }
        Write-Host "   $($profile.Name): $($profile.Enabled) (入站: $($profile.DefaultInboundAction))" -ForegroundColor Gray
    }
    
    if ($blockingRules -gt 0) {
        Write-Host "   ⚠ 防火墙可能阻止某些连接" -ForegroundColor Yellow
    } else {
        Write-Host "   ✓ 防火墙设置正常" -ForegroundColor Green
    }
    
    # 8. 检查可能影响网络的程序
    Write-Host "8. 检查可能影响网络的程序..." -ForegroundColor Cyan
    $networkPrograms = @("360", "qq", "baidu", "tencent", "kaspersky", "avast", "norton", "vpn", "proxy")
    $foundPrograms = @()
    
    Get-Process | ForEach-Object {
        $processName = $_.ProcessName.ToLower()
        foreach ($program in $networkPrograms) {
            if ($processName -like "*$program*") {
                $foundPrograms += $_.ProcessName
                break
            }
        }
    }
    
    if ($foundPrograms) {
        Write-Host "   ⚠ 发现可能影响网络的程序:" -ForegroundColor Yellow
        $foundPrograms | Sort-Object -Unique | ForEach-Object { Write-Host "     - $_" -ForegroundColor Gray }
        Write-Host "   建议检查这些程序的网络设置或临时关闭" -ForegroundColor Yellow
    } else {
        Write-Host "   ✓ 未发现可疑程序" -ForegroundColor Green
    }
    
    # 9. 网络连接测试
    Write-Host "9. 测试网络连接..." -ForegroundColor Cyan
    $testSites = @(
        @{Name="Google DNS"; Address="8.8.8.8"; Port=53},
        @{Name="CloudFlare DNS"; Address="1.1.1.1"; Port=53},
        @{Name="百度"; Address="www.baidu.com"; Port=80},
        @{Name="Google"; Address="www.google.com"; Port=80}
    )
    
    foreach ($site in $testSites) {
        try {
            $result = Test-NetConnection -ComputerName $site.Address -Port $site.Port -InformationLevel Quiet -WarningAction SilentlyContinue
            if ($result) {
                Write-Host "   ✓ $($site.Name) 连接正常" -ForegroundColor Green
            } else {
                Write-Host "   ✗ $($site.Name) 连接失败" -ForegroundColor Red
            }
        } catch {
            Write-Host "   ✗ $($site.Name) 测试出错" -ForegroundColor Red
        }
    }
    
    # 10. 重启浏览器
    Write-Host "10. 重启浏览器..." -ForegroundColor Cyan
    $browsers = @("chrome", "firefox", "msedge", "iexplore", "opera", "brave")
    $closedBrowsers = @()
    
    foreach ($browser in $browsers) {
        $processes = Get-Process -Name $browser -ErrorAction SilentlyContinue
        if ($processes) {
            $processes | Stop-Process -Force
            $closedBrowsers += $browser
            Write-Host "   ✓ 已关闭 $browser" -ForegroundColor Green
        }
    }
    
    if ($closedBrowsers) {
        Write-Host "   等待3秒后重新启动浏览器..." -ForegroundColor White
        Start-Sleep 3
        
        # 重新启动Chrome(如果之前运行过)
        if ($closedBrowsers -contains "chrome") {
            try {
                Start-Process "chrome" -ErrorAction SilentlyContinue
                Write-Host "   ✓ Chrome已重新启动" -ForegroundColor Green
            } catch {
                Write-Host "   ⚠ Chrome重新启动失败,请手动启动" -ForegroundColor Yellow
            }
        }
    } else {
        Write-Host "   ✓ 未发现正在运行的浏览器" -ForegroundColor Green
    }
    
    # 修复完成总结
    Write-Host ""
    Write-Host "======================================" -ForegroundColor Yellow
    Write-Host "修复完成!" -ForegroundColor Green
    Write-Host ""
    Write-Host "已执行的修复操作:" -ForegroundColor White
    Write-Host "- 禁用了系统代理设置" -ForegroundColor Gray
    Write-Host "- 清除了DNS缓存" -ForegroundColor Gray
    Write-Host "- 重置了Winsock和TCP/IP" -ForegroundColor Gray
    Write-Host "- 重启了浏览器进程" -ForegroundColor Gray
    Write-Host ""
    Write-Host "建议操作:" -ForegroundColor White
    Write-Host "1. 重启计算机使网络重置完全生效" -ForegroundColor Yellow
    Write-Host "2. 如果问题仍存在,检查路由器/调制解调器" -ForegroundColor Yellow
    Write-Host "3. 临时关闭防病毒软件测试" -ForegroundColor Yellow
    Write-Host "4. 检查网络供应商是否有故障" -ForegroundColor Yellow
    
    # 询问是否重启
    Write-Host ""
    $reboot = Read-Host "是否现在重启计算机?(Y/N)"
    if ($reboot -eq 'Y' -or $reboot -eq 'y') {
        Write-Host "正在重启计算机..." -ForegroundColor Green
        Restart-Computer -Force
    }
    
} catch {
    Write-Host "脚本执行出错: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""
Write-Host "按任意键退出..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。