Powershell auto reconnect Windows VPN Connections.
Here’s a quick PowerShell script that allows auto-reconnect of Windows VPN connections without having to use VPN Re-dial / Re-connect. You could also use this script to automatically connect the VPN at logon or to stop users disconnecting the VPN manually to bypass company controls.
$vpnName = "<VPN Name>"; $vpn = Get-VpnConnection -Name $vpnName; while($vpn.ConnectionStatus -eq "Disconnected"){ rasdial $vpnName "username" "password"; Start-Sleep -Seconds 120 }
Make sure to update the VPN Name, the username & password.
To get the script to auto-reconnect we want to create a Windows Scheduled task with an event viewer trigger
The important things here are
Log: System
Source: Rasman
EventID: 20268.
This will cause the script to trigger ever time there is a disconnect event.
The latest version of this script can always be found here – https://github.com/ExtendedIT/PowerShell/tree/master/VPN-Reconnection
For any help or custom scripts email – [email protected]