Created
January 19, 2021 14:48
Star
You must be signed in to star a gist
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 | |
} | |
} |
PS C:\> .\vpn_fix_wsl_routes.ps1
Get-NetRoute : Cannot validate argument on parameter 'InterfaceIndex'. The argument is null. Provide a valid value for
the argument, and then try running the command again.
At C:\vpn_fix_wsl_routes.ps1:27 char:60
+ $routes = Get-NetRoute -AddressFamily IPv4 -InterfaceIndex $idx_vpn
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-NetRoute], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Get-NetRoute
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
CheckPoint_VPN_fix_wsl_routes
Powershell script to fix internet connection issue with WSL2 and CheckPoint VPN. It rewrites the route table in your Windows host machine.
How To
Just run the script in the Windows Powershell as an Administrator:
.\vpn_fix_wsl_routes.ps1
You need to run it each time when you was connected by the CheckPoint VPN.
If it dosn't work, make sure you run it as Administrator. By default, we suppose that the Name of your WSL2 Network Interface is vEthernet (WSL) and InterfaceDescription of your CheckPoint Interface has "Check Point" words.