Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / Windows 10 / Windows: Auto Reconnect to VPN on Disconnect

November 3, 2023 PowerShellWindows 10Windows 11Windows Server 2019

Windows: Auto Reconnect to VPN on Disconnect

By default, the built-in Windows VPN client does not attempt to reconnect to the VPN server if the connection is lost. If the connection is dropped, the user must manually reconnect to the VPN server. Windows has several tools that you can use to set up automatic VPN reconnection after a connection loss.

Reconnect settings are missing from the Windows VPN client GUI. However, you can configure them using the rasphone.pbk configuration file stored in the user profile (%userprofile%\AppData\Roaming\Microsoft\Network\Connections\Pbk). The settings of all user VPN connection profiles are stored in this file.

VPN connection profiles, which are available to all Windows users, are stored in the %ProgramData%\Microsoft\Network\Connections\Pbk\rasphone.pbk file.

Open the rasphone.pbk file with a text editor, look for the section of the VPN profile you want to use (for example [L2TP-VPN]), and scroll down to the following options:

  • RedialAttempts=3 – number of reconnect attempts (maximum 99)
  • RedialSeconds=60 – seconds between reconnect attempts
  • IdleDisconnectSeconds=0 — the waiting time before it becomes clear that a connection has been lost (0 –don’t wait)
  • RedialOnLinkFailure=1 — redial if the connection fails (1 – yes, 0 – no)

rasphone.pbk redial options

However, the rasphone.pbk settings may not work correctly in all cases. It is therefore recommended that you automate VPN reconnection using a scheduler task that reconnects to the VPN when the connection is lost or when the computer is connected to the network (Wi-Fi or Ethernet). We are interested in the following events

EventIDSourceEvent description
20226RasClient (Application log)The user username dialed a connection named VPNConnectionName which has terminated. The reason code returned on termination is 829.
10000NetworkProfile (Microsoft-Windows-NetworkProfile/Operational log)Network Connected

event id 20026 - vpn connection dropped

Open Windows Task Scheduler (taskschd.msc) and create a new task. Set a task name: AutoReconnectVPN.

Go to the Triggers tab and create two event triggers (Begin the task: On event):

  1. Log: Application , Source: RasClient , EventID: 20226
  2. Log: Microsoft-Windows-NetworkProfile/Operational , Source: NetworkProfile , EventID: 10000

Create reconnection triggers in Task Scheduler

Now you need to configure the action to be taken when one of these events occurs. Go to the Actions tabs -> click New.

  • Action: Start a program
  • Program/script: %windir%\System32\rasdial.exe
  • Argument: YourVPNCOnnectionName

task scheduler - reconnect to vpn automatically

In this example, a VPN profile with a saved password is used for the connection. To explicitly specify the VPN connection name and password, set them as follows:

"YourVPNCOnnectionName" username password

You can list VPN connections on Windows using PowerShell:

Get-VpnConnection|select name,ServerAddress,TunnelType

powershell - list vpn profiles on windows

Test the scheduler task: temporarily unplug the network cable from the computer, disable the network adapter, or disconnect the VPN. Check that Windows automatically reconnects to your VPN server when the network connection is restored.

Previously, we showed you how to configure a similar task to automatically disconnect from Wi-Fi when your computer is connected to an Ethernet network.

1 comment
0
Facebook Twitter Google + Pinterest
previous post
SCCM and WMI Query to Find All Laptops and Desktops
next post
Configuring Remote Control in SCCM 2012

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

November 7, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

1 comment

George January 25, 2018 - 8:14 pm

Thanks SO much for this guide. After upgrading from W7 to W10, this was bugging the hell out of me. Thank to this, I was even able to create a “VPN Alternate” task, which looks for an 809 Error Code (ERROR_VPN_TIMEOUT aka VPN server unresponsive) in the Event Log and then dial an alternate VPN server. I simply created the same Tricky Task as above, but edited this line of the XML query:
*[EventData[Data[4]=’829′ or ‘629’]]

…to this:
*[EventData[Data[4]=’809′]]

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Run MySQL Queries from PowerShell
  • PowerShell Remoting via WinRM for Non-Admin Users
  • How to Clear Event Viewer Logs on Windows
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top