Created
January 19, 2021 14:48
-
-
Save bastaramus/3d5f1326835fe111d5ea9684c41a6675 to your computer and use it in GitHub Desktop.
Powershell script to fix internet connection issue with WSL2 and CheckPoint VPN
This file contains 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
[IPAddress]$IP_wsl = (Get-NetIPAddress -InterfaceAlias "vEthernet (WSL)" -AddressFamily "IPv4" | Select-Object IPAddress).ipaddress | |
$PrefixLength_wsl = (Get-NetIPAddress -InterfaceAlias "vEthernet (WSL)" -AddressFamily "IPv4" | Select-Object PrefixLength).prefixlength | |
$idx_vpn = (Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Check Point"} | Select-Object ifIndex).ifIndex | |
Function CIDRToNetMask { | |
[CmdletBinding()] | |
Param( | |
[ValidateRange(0,32)] | |
[int16]$PrefixLength=0 | |
) | |
$bitString=('1' * $PrefixLength).PadRight(32,'0') | |
$strBuilder=New-Object -TypeName Text.StringBuilder | |
for($i=0;$i -lt 32;$i+=8){ | |
$8bitString=$bitString.Substring($i,8) | |
[void]$strBuilder.Append("$([Convert]::ToInt32($8bitString,2)).") | |
} | |
return $strBuilder.ToString().TrimEnd('.') | |
} | |
[IPAddress]$NetMask = CIDRToNetMask($PrefixLength_wsl) | |
$NetAddress_wsl = [ipaddress]($IP_wsl.Address -band $NetMask.Address) | |
$routes = Get-NetRoute -AddressFamily IPv4 -InterfaceIndex $idx_vpn | |
foreach ($route in $routes) { | |
$dst_address = [ipaddress]$route.DestinationPrefix.split('/')[0] | |
$netaddress_dst = [ipaddress]($dst_address.Address -band $NetMask.Address) | |
if ($netaddress_dst.IPAddressToString -eq $NetAddress_wsl.IPAddressToString) { | |
Remove-NetRoute -InputObject $route -Confirm:$false | |
} | |
} |
PavelFranta
commented
Mar 2, 2021
I'm having the same issue with InterfaceIndex
Worked for me. Thank you so much!
Worked for me!! Thanks!
It works for me! Thanks.
I needed to change the execution policy
PS C:\Users\LINQ> Get-ExecutionPolicy
Restricted
PS C:\Users\LINQ> Set-ExecutionPolicy RemoteSigned
PS C:\Users\LINQ> Get-ExecutionPolicy
RemoteSigned
Confirming that it works for me as well.
If you don't want to change your execution policy, you can run this command before the script:
PS> Unblock-File -Path path\to\script\vpn_fix_wsl_routes.ps1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment