730 字
4 分钟
Windows 断网修复脚本
Windows 断网修复脚本
@echo offsetlocal EnableExtensions EnableDelayedExpansiontitle Windows Network Deep Repair (Windows 10/11)
:: --- Self-elevate to admin ---net session >NUL 2>&1if errorlevel 1 ( echo Requesting administrative privileges... powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs" exit /b)
:: --- Options (1=enable, 0=disable) ---set "ENABLE_FIREWALL_RESET=1"set "ENABLE_HOSTS_RESET=1"set "ENABLE_DELETE_WIFI_PROFILES=0" :: if set to 1, ALL saved Wi-Fi profiles are removedset "ENABLE_HEALTH_REPAIR=1" :: DISM + SFC
:: --- Log file ---for /f %%i in ('powershell -NoProfile -Command "(Get-Date).ToString(\"yyyyMMdd_HHmmss\")"') do set "TS=%%i"set "LOG=%TEMP%\Net_DeepRepair_%TS%.log"echo --- Windows Network Deep Repair started %DATE% %TIME% --- > "%LOG%"call :log "Logging to: %LOG%"
:: --- Inventory / snapshot ---call :log "Collecting environment info..."ver >> "%LOG%"ipconfig /all >> "%LOG%"route print >> "%LOG%"netsh winsock show catalog >> "%LOG%" 2>&1netsh winhttp show proxy >> "%LOG%" 2>&1
:: --- Clear user & system proxies ---call :log "Resetting system and user proxy settings..."reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f >> "%LOG%" 2>&1reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /f >> "%LOG%" 2>&1reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v AutoConfigURL /f >> "%LOG%" 2>&1netsh winhttp reset proxy >> "%LOG%" 2>&1
:: --- Reset HOSTS to Windows default (backup first) ---if "%ENABLE_HOSTS_RESET%"=="1" ( call :log "Resetting HOSTS file to Windows default (backup first)..." set "HOSTS=%SystemRoot%\System32\drivers\etc\hosts" if exist "%HOSTS%" copy /y "%HOSTS%" "%HOSTS%.bak.%TS%" >nul 2>&1 attrib -r -s -h "%HOSTS%" >nul 2>&1 ( echo # Default Windows HOSTS file echo # localhost name resolution is handled within DNS itself. echo # 127.0.0.1 localhost echo # ::1 localhost ) > "%HOSTS%")
:: --- Reset Winsock & TCP/IP (IPv4/IPv6) ---call :log "Resetting Winsock and TCP/IP (IPv4/IPv6)..."netsh winsock reset >> "%LOG%" 2>&1netsh int ipv4 reset "%TEMP%\ipv4_reset_%TS%.log" >> "%LOG%" 2>&1netsh int ipv6 reset "%TEMP%\ipv6_reset_%TS%.log" >> "%LOG%" 2>&1
:: --- Flush DNS, clear ARP, clear routes, renew IP ---call :log "Clearing DNS, ARP cache and routing table; renewing IP..."ipconfig /flushdns >> "%LOG%" 2>&1arp -d * >> "%LOG%" 2>&1route -f >> "%LOG%" 2>&1ipconfig /release >> "%LOG%" 2>&1ipconfig /renew >> "%LOG%" 2>&1
:: --- Ensure DHCP and automatic DNS on all active adapters ---call :log "Ensuring DHCP and automatic DNS on all adapters..."powershell -NoProfile -ExecutionPolicy Bypass -Command ^"Get-NetIPInterface -AddressFamily IPv4 | Where-Object { $_.Dhcp -ne 'Enabled' } | ForEach-Object { Set-NetIPInterface -Dhcp Enabled -InterfaceIndex $_.InterfaceIndex }" >> "%LOG%" 2>&1powershell -NoProfile -ExecutionPolicy Bypass -Command ^"Get-DnsClient | Where-Object { $_.InterfaceAlias -notmatch 'vEthernet|Hyper-V|VMware|VirtualBox|TAP|VPN|Loopback|WSL|Bluetooth' } | ForEach-Object { Set-DnsClientServerAddress -InterfaceAlias $_.InterfaceAlias -ResetServerAddresses }" >> "%LOG%" 2>&1
:: --- Restart critical networking services ---call :log "Starting critical networking services..."for %%S in (bfe mpssvc Dhcp Dnscache nsi netprofm LanmanWorkstation LanmanServer iphlpsvc WinHttpAutoProxySvc) do ( sc config %%S start= auto >> "%LOG%" 2>&1 net start %%S >> "%LOG%" 2>&1)
:: --- Backup & reset Windows Defender Firewall (optional) ---if "%ENABLE_FIREWALL_RESET%"=="1" ( call :log "Backing up and resetting Windows Defender Firewall to defaults..." netsh advfirewall export "%TEMP%\FirewallPolicy_%TS%.wfw" >> "%LOG%" 2>&1 netsh advfirewall reset >> "%LOG%" 2>&1)
:: --- Remove ALL saved Wi‑Fi profiles (optional deep cleanup) ---if "%ENABLE_DELETE_WIFI_PROFILES%"=="1" ( call :log "Deleting ALL saved Wi‑Fi profiles (you will need to re-enter Wi‑Fi passwords)..." netsh wlan delete profile name=* >> "%LOG%" 2>&1)
:: --- Component store & system files repair (optional) ---if "%ENABLE_HEALTH_REPAIR%"=="1" ( call :log "Repairing component store (DISM) and system files (SFC)..." DISM /Online /Cleanup-Image /RestoreHealth >> "%LOG%" 2>&1 sfc /scannow >> "%LOG%" 2>&1)
:: --- Final quick checks ---call :log "Final connectivity tests..."ping -n 2 1.1.1.1 >> "%LOG%" 2>&1 && call :log "ICMP to 1.1.1.1: OK" || call :log "ICMP to 1.1.1.1: FAILED"nslookup www.microsoft.com >> "%LOG%" 2>&1 && call :log "DNS lookup (www.microsoft.com): OK" || call :log "DNS lookup (www.microsoft.com): FAILED"
call :log "Complete. A restart is recommended to finalize stack resets."echo.echo ===============================================================================echo Windows Network Deep Repair is complete.echo Log file: %LOG%echo A restart is recommended to finalize Winsock/IP resets.echo ===============================================================================echo.choice /C YN /N /T 15 /D N /M "Reboot now? [Y/N] "if errorlevel 2 goto :endif errorlevel 1 shutdown /r /t 5 /c "Windows Network Deep Repair completed"goto :eof
:logset "MSG=%~1"echo [%DATE% %TIME%] %MSG%>>"%LOG%" echo [%DATE% %TIME%] %MSG%goto :eof
:endexit /b 0 Windows 断网修复脚本
https://twenhub.com/posts/windows-duan-wang-xiu-fu-jiao-ben/