Skip to content

Instantly share code, notes, and snippets.

@pirafrank
Last active May 20, 2024 10:16
Show Gist options
  • Save pirafrank/edda727d1ce80b46d0492987bc745dcd to your computer and use it in GitHub Desktop.
Save pirafrank/edda727d1ce80b46d0492987bc745dcd to your computer and use it in GitHub Desktop.
Restore Internet access and restore WSL 2 network after connecting to Pulse-Ivanti networks. Tested on Windows 11 23H2 and WSL 2 (v.2.1.5.0)
# Define the interface names and the two DNS IPs
# edit the followin two lines
$internet_interface1 = "Ethernet 6"
$internet_interface2 = "Wi-Fi"
##### do not edit below this line #####
$dns1 = "1.1.1.1"
$dns2 = "10.19.56.151"
# Check if running as administrator
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Please run this script as an administrator."
Exit
}
# Get the network interfaces
$interfaces = Get-NetAdapter | Select-Object -Property Name, Status
# Output the interfaces
$interfaces | Format-Table -AutoSize
# Find the interfaces that match the names stored in $internet_interface1 and $internet_interface2
$target_interface1 = $interfaces | Where-Object { $_.Name -eq $internet_interface1 }
$target_interface2 = $interfaces | Where-Object { $_.Name -eq $internet_interface2 }
$dns_update = $false
# If the first interface is found and is up, update the DNS settings
if ($target_interface1 -and $target_interface1.Status -eq 'Up') {
Write-Host "Updating DNS settings for $internet_interface1..."
Set-DnsClientServerAddress -InterfaceAlias $internet_interface1 -ServerAddresses $dns1, $dns2
Write-Host "DNS settings updated"
$dns_update = $true
}
# If the second interface is found and is up, update the DNS settings
elseif ($target_interface2 -and $target_interface2.Status -eq 'Up') {
Write-Host "Updating DNS settings for $internet_interface2..."
Set-DnsClientServerAddress -InterfaceAlias $internet_interface2 -ServerAddresses $dns1, $dns2
Write-Host "DNS settings updated"
$dns_update = $true
}
else {
Write-Host "Neither $internet_interface1 nor $internet_interface2 is up"
}
if ($dns_update) {
Write-Host "Starting wsl-vpnkit to restore WSL 2 network functionality..."
Write-Host "IMPORTANT: Do NOT close the PowerShell window that is about to open until you disconnect from the VPN."
# install wsl-vpnkit first from https://github.com/sakai135/wsl-vpnkit
# Open a new PowerShell session without admin rights and run the wsl-vpnkit command
Start-Process powershell.exe { wsl.exe -d wsl-vpnkit --cd /app wsl-vpnkit }
Write-Host "wsl-vpnkit started"
Write-Host ""
Write-Host "Note: check your WSL2 DNS settings to ensure they are correct."
Write-Host "/etc/resolv.conf should contain, in this order, 1.1.1.1 and the Windows Host IP in the WSL network as nameservers."
Write-Host ""
Write-Host "Also add the following to /etc/wsl.conf to avoid wsl.exe messing up DNS settings inside your WSL distro:"
Write-Host "[network]"
Write-Host "generateResolvConf=false"
Write-Host ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment