Skip to content

Instantly share code, notes, and snippets.

@bastaramus
Created January 19, 2021 14:48
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastaramus/3d5f1326835fe111d5ea9684c41a6675 to your computer and use it in GitHub Desktop.
Save bastaramus/3d5f1326835fe111d5ea9684c41a6675 to your computer and use it in GitHub Desktop.
Powershell script to fix internet connection issue with WSL2 and CheckPoint VPN
[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
}
}
@allanmedeiros71
Copy link

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

@PeterShaws
Copy link

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

Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment