Last active
November 20, 2025 08:57
-
-
Save mdimai666/7a0fcd715df6453a4260177f95119707 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Скрипт Чинит отвалившующся сеть WSL | |
| # Запускаем от имени администратора. | |
| Write-Output "---------------------- FIX WSL INTERNET ----------------------" | |
| # 1. Завершаем все дистрибутивы WSL | |
| Write-Output "Shutting down WSL..." | |
| wsl --shutdown | |
| # $wslEnthernetAdapterName = "vEthernet (WSL)" | |
| # $wslEnthernetAdapterName = "vEthernet (WSL (Hyper-V firewall))" | |
| Write-Output "Detecting WSL adapter..." | |
| $adapter = Get-NetAdapter | Where-Object { | |
| $_.InterfaceDescription -like "*WSL*" -or | |
| $_.Name -like "*WSL*" | |
| } | Select-Object -First 1 | |
| if ($adapter) { | |
| $wslEnthernetAdapterName = $adapter.Name; | |
| Write-Output "adapter: ${wslEnthernetAdapterName}"; | |
| } | |
| else { | |
| throw "WSL сетевой адаптер не найден" | |
| } | |
| # 2. Определяем подсеть адаптера "vEthernet (WSL)" | |
| Write-Output "Detecting WSL network..." | |
| $ifconfig = Get-NetIPAddress -InterfaceAlias $wslEnthernetAdapterName -AddressFamily IPv4 | |
| if (-not $ifconfig) { | |
| Write-Error "Не найден интерфейс vEthernet (WSL). Проверь, что WSL установлен и запущен хотя бы раз." | |
| exit 1 | |
| } | |
| $ip_text = $ifconfig.IPAddress; | |
| Write-Output "IP: $ip_text" | |
| $prefix = $ifconfig.PrefixLength | |
| $ip = $ifconfig[0].IPAddress.Trim() | |
| $octets = $ip -split '\.' | |
| $networkBase = ($octets[0..2] -join '.') + ".0" | |
| $baseIp = [System.Net.IPAddress]::Parse($networkBase) | |
| $subnet = "$($baseIp)/$prefix" | |
| Write-Output "WSL subnet detected: $subnet" | |
| if (1) { | |
| # 3. Восстанавливаем NAT | |
| $natName = "WSL" | |
| Write-Output "Re-creating NAT rule..." | |
| Remove-NetNat -Name $natName -Confirm:$false -ErrorAction SilentlyContinue | |
| New-NetNat -Name $natName -InternalIPInterfaceAddressPrefix $subnet | |
| # 4. Перезапуск WinNAT | |
| Write-Output "Restarting WinNAT..." | |
| net stop winnat | |
| net start winnat | |
| } | |
| Write-Output "---------------------- DONE ----------------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment