hQuse: Auto reconnect VPN in windows 10

Publicēts: 15:31:33 01/03/22
Autors: hQuse

Simple howto.

No additional third side software required. Windows built-in task scheduler used. Username and password arguments NOT required for command.

This assumes you're using Windows 10 built-in VPN and already saved VPN connection (saved username and password). Probably will work on older Windows versions too.

First of all find saved VPN connection parameters file C:\Users\YOURUSERNAME\AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone.pbk

Make backup.

Then open original and find [YOURVPNNAME].

Find PreviewUserPw=1
Change it to PreviewUserPw=0

Save the file.


Next create new file, copy following content, change keywords in CAPITALLETTERS.

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-12-30T14:40:51.0713534</Date>
<Author>DOMAIN\YOURUSERNAME</Author>
<URI>\VPN reconnect task</URI>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Application"&gt;&lt;Select Path="Application"&gt;*[System[Provider[@Name='RasClient'] and EventID=20226]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"&gt;&lt;Select Path="Microsoft-Windows-NetworkProfile/Operational"&gt;*[System[Provider[@Name='NetworkProfile'] and EventID=10000]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
</Triggers>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\rasphone.exe</Command>
<Arguments>-d "YOURVPNNAME" -f C:\Users\YOURUSERNAME\AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone.pbk</Arguments>
</Exec>
</Actions>
</Task>

Next open Windows Task Scheduler and import this file. New task will be created.

Try to Run/Enable it.

Connect/Disconnect VPN.

If setup is correct, VPN should reconnect right after disconnection.