Last active
April 25, 2025 12:19
-
-
Save bisand/2f8c96fbf1f6625a1781f830472ad762 to your computer and use it in GitHub Desktop.
Fix network problems after Check Point Endpoint Security VPN connect and disconnect
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
@echo off | |
start "" /min "C:\Program Files\PowerShell\7\pwsh.exe" -WindowStyle Hidden -ExecutionPolicy Bypass -Command "& '~\AutoFixVpn.ps1'" |
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
Add-Type -AssemblyName System.Windows.Forms | |
Add-Type -AssemblyName System.Drawing | |
$mainAdapter = "Ethernet" | |
$vpnIpPrefix = "192.168.59." | |
$vpnWasConnected = $false | |
$logFile = "$PSScriptRoot\vpn-fix.log" | |
$vpnAdapterName = "Ethernet 2" # Name of your VPN adapter (adjust accordingly) | |
# -- Define the monitoring action | |
$timer = New-Object System.Windows.Forms.Timer | |
$timer.Interval = 5000 # 5 seconds | |
# -- Function to check VPN status by looking for an IP address | |
function Is-VpnConnected { | |
$vpnAdapter = Get-NetAdapter | Where-Object { $_.Name -eq $vpnAdapterName } | |
if ($vpnAdapter.Status -eq 'Up') { | |
# Check if the adapter has a specific VPN IP address | |
$vpnIp = (Get-NetIPAddress -InterfaceAlias $vpnAdapterName | Where-Object { $_.IPAddress -like "$vpnIpPrefix*" }).IPAddress | |
if ($vpnIp) { | |
return $true | |
} | |
} | |
return $false | |
} | |
function Get-IconFromShell32 { | |
param( | |
[int]$iconIndex = 0 | |
) | |
$shell32 = "$env:SystemRoot\System32\SHELL32.dll" | |
$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($shell32) | |
# Use ExtractIcon via Win32 API for indexed icons | |
$signature = @' | |
[DllImport("shell32.dll", CharSet = CharSet.Auto)] | |
public static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex); | |
'@ | |
Add-Type -MemberDefinition $signature -Name 'Win32Icon' -Namespace 'Win32' | |
$hIcon = [Win32.Win32Icon]::ExtractIcon([IntPtr]::Zero, $shell32, $iconIndex) | |
if ($hIcon -eq [IntPtr]::Zero) { return $null } | |
return [System.Drawing.Icon]::FromHandle($hIcon) | |
} | |
function Log($msg) { | |
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
"$timestamp $msg" | Out-File -Append $logFile | |
Write-Host "$timestamp $msg" | |
} | |
# -- Create tray icon | |
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon | |
$notifyIcon.Icon = Get-IconFromShell32 -iconIndex 15 # Default icon (VPN connected) | |
$notifyIcon.Text = "VPN Fix Monitor" | |
$notifyIcon.Visible = $true | |
$vpnNowConnected = Is-VpnConnected | |
# -- Use a global scope to ensure persistence across timer ticks | |
$global:vpnWasConnected = $vpnNowConnected | |
$global:timer = $timer | |
# -- Function to change tray icon based on VPN status | |
function UpdateTrayIcon($isVpnConnected) { | |
# Overlay icon logic: base icon for VPN status, overlay for monitoring state | |
$baseIconIndex = if ($isVpnConnected) { 14 } else { 15 } | |
$baseIcon = Get-IconFromShell32 $baseIconIndex | |
# Overlay: green dot for monitoring ON, red dot for OFF | |
$overlayColor = if ($global:timer.Enabled) { 'LimeGreen' } else { 'Red' } | |
$overlaySize = 12 | |
# Create a new bitmap and draw base icon + overlay | |
$bmp = New-Object System.Drawing.Bitmap $baseIcon.Width, $baseIcon.Height | |
$g = [System.Drawing.Graphics]::FromImage($bmp) | |
$g.DrawIcon($baseIcon, 0, 0) | |
# Draw overlay circle at bottom-right | |
$overlayBrush = New-Object System.Drawing.SolidBrush ([System.Drawing.Color]::$overlayColor) | |
$x = $bmp.Width - $overlaySize - 2 | |
$y = $bmp.Height - $overlaySize - 2 | |
$g.FillEllipse($overlayBrush, $x, $y, $overlaySize, $overlaySize) | |
$g.Dispose() | |
# Set the tray icon | |
$iconWithOverlay = [System.Drawing.Icon]::FromHandle($bmp.GetHicon()) | |
$notifyIcon.Icon = $iconWithOverlay | |
# Tooltip text | |
$monitoringStatus = if ($global:timer.Enabled) { "Monitoring ON" } else { "Monitoring OFF" } | |
$vpnStatus = if ($isVpnConnected) { "VPN Connected" } else { "VPN Disconnected" } | |
$notifyIcon.Text = "$vpnStatus ($monitoringStatus)" | |
} | |
# -- Function to show tray notifications | |
function ShowTrayNotification($title, $message, $icon) { | |
$notifyIcon.ShowBalloonTip(3000, $title, $message, $icon) | |
} | |
$timer.Add_Tick({ | |
$vpnNowConnected = Is-VpnConnected | |
# Log the current VPN connection status | |
# Log "VPN Adapter status: $($vpnNowConnected)" | |
# Only restart if VPN connection has changed | |
if ($vpnNowConnected -and -not $global:vpnWasConnected) { | |
Log "π’ VPN connected β restarting adapter..." | |
Restart-NetAdapter -Name $mainAdapter -Confirm:$false | |
$notifyIcon.ShowBalloonTip(5000, "VPN Connected", "VPN connection established. Restarting adapter...", [System.Windows.Forms.ToolTipIcon]::Info) | |
Start-Sleep -Seconds 2 # Add a small delay before continuing | |
$global:vpnWasConnected = $true | |
UpdateTrayIcon $true # Change tray icon to reflect VPN connection | |
} | |
elseif (-not $vpnNowConnected -and $global:vpnWasConnected) { | |
Log "π΄ VPN disconnected β restarting adapter..." | |
Restart-NetAdapter -Name $mainAdapter -Confirm:$false | |
$notifyIcon.ShowBalloonTip(5000, "VPN Disconnected", "VPN disconnected. Restarting adapter...", [System.Windows.Forms.ToolTipIcon]::Warning) | |
Start-Sleep -Seconds 2 # Add a small delay before continuing | |
$global:vpnWasConnected = $false | |
UpdateTrayIcon $false # Change tray icon to reflect VPN disconnection | |
} | |
}) | |
# -- Menu items | |
$menu = New-Object System.Windows.Forms.ContextMenuStrip | |
$startItem = $menu.Items.Add("β Start Monitoring") | |
$stopItem = $menu.Items.Add("βΉοΈ Stop Monitoring") | |
$openLogItem = $menu.Items.Add("π Open Log File") | |
$restartNetwork = $menu.Items.Add("π Restart Network Adapter") | |
$exitItem = $menu.Items.Add("Exit") | |
$startItem.Add_Click({ | |
$global:timer.Start(); | |
Log "β Monitoring started" | |
UpdateTrayIcon $global:vpnWasConnected | |
}) | |
$stopItem.Add_Click({ | |
$global:timer.Stop(); | |
Log "βΉοΈ Monitoring stopped" | |
UpdateTrayIcon $global:vpnWasConnected | |
}) | |
$restartNetwork.Add_Click({ | |
Log "π Restarting network adapter..." | |
Restart-NetAdapter -Name $mainAdapter -Confirm:$false | |
$notifyIcon.ShowBalloonTip(5000, "Network Restart", "Netwoek adapter restarted.", [System.Windows.Forms.ToolTipIcon]::Info) | |
}) | |
$openLogItem.Add_Click({ | |
Invoke-Item $logFile | |
}) | |
$exitItem.Add_Click({ | |
$global:timer.Stop() | |
$notifyIcon.Dispose() | |
Log "π Script exited" | |
[System.Windows.Forms.Application]::Exit() | |
}) | |
$notifyIcon.ContextMenuStrip = $menu | |
$timer.Start(); | |
# -- Check VPN status when script starts | |
if ($vpnNowConnected) { | |
Log "π’ VPN connected on startup β no restart needed." | |
UpdateTrayIcon $true | |
} else { | |
Log "π΄ VPN not connected on startup" | |
UpdateTrayIcon $false | |
} | |
Log "β Monitoring started" | |
# -- Keep the tray app running | |
Log "π’ VPN Fix tray app started" | |
[System.Windows.Forms.Application]::Run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am running Windows 11 as a virtual machine using VirtualBox with custom NAT networking configured. When connecting to and disconnecting form a VPN endpoint using Check Point Endpoint Security VPN, network connection is lost.
The purpose of this script is to run in the background and detect whenever VPN has connected or disconnected. When it detects changes it restarts the network adapter to restore the lost network connection.
Use AutFixVpn.cmd to run hidden in the background