Forum Discussion
Show a notification when VPN connection disconnects on its own - built in Windows 10 connection
- Mar 08, 2023
I fully agree this should be standard functionality in windows. But despite 3 years wait and a new OS (W11) still no joy...
I've found a workaround which works really well. I'm using task scheduler to trigger when VPN disconnects and show a messagebox.
Please find code below. Save to a XML file. In task scheduler - press import and select XML file. Change the user account to your own.<?xml version="1.0" encoding="UTF-16"?> <Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> <RegistrationInfo> <Date>2023-02-07T12:16:02.5177569</Date> <Author>vervlogen</Author> <URI>\VPN disconnect warning</URI> </RegistrationInfo> <Triggers> <EventTrigger> <Enabled>true</Enabled> <Subscription><QueryList><Query Id="0" Path="System"><Select Path="System">*[System[Provider[@Name='Rasman'] and EventID=20268]]</Select></Query></QueryList></Subscription> </EventTrigger> </Triggers> <Principals> <Principal id="Author"> <UserId>S-1-5-21-1234567890-12345678-123456789-1001</UserId> <LogonType>InteractiveToken</LogonType> <RunLevel>LeastPrivilege</RunLevel> </Principal> </Principals> <Settings> <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> <DisallowStartIfOnBatteries>false</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> <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession> <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine> <WakeToRun>false</WakeToRun> <ExecutionTimeLimit>PT72H</ExecutionTimeLimit> <Priority>7</Priority> </Settings> <Actions Context="Author"> <Exec> <Command>run-hidden</Command> <Arguments>powershell -WindowStyle hidden -Command "& {Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.Interaction]::MsgBox('The VPN has disconnected','OKOnly,SystemModal,Information', 'VPN Disconnect')}"</Arguments> </Exec> </Actions> </Task>
I fully agree this should be standard functionality in windows. But despite 3 years wait and a new OS (W11) still no joy...
I've found a workaround which works really well. I'm using task scheduler to trigger when VPN disconnects and show a messagebox.
Please find code below. Save to a XML file. In task scheduler - press import and select XML file. Change the user account to your own.
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2023-02-07T12:16:02.5177569</Date>
<Author>vervlogen</Author>
<URI>\VPN disconnect warning</URI>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription><QueryList><Query Id="0" Path="System"><Select Path="System">*[System[Provider[@Name='Rasman'] and EventID=20268]]</Select></Query></QueryList></Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-21-1234567890-12345678-123456789-1001</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</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>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>run-hidden</Command>
<Arguments>powershell -WindowStyle hidden -Command "& {Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.Interaction]::MsgBox('The VPN has disconnected','OKOnly,SystemModal,Information', 'VPN Disconnect')}"</Arguments>
</Exec>
</Actions>
</Task>
Hi Vervlogen,
Thank-you very much for posting your work-around notification script.
I am fundamentally computer illiterate and don't know what to change in the script to make it work
ie. "Change the user account to your own"
What exactly should I change? I've tried changing the author to my login account name but that didn't work. Would really appreciate your help!
Many thanks,
Bleep
- HotCakeXSep 05, 2023MVP
You need to first get your user account's SID:
(Get-CimInstance -Class win32_userAccount -Filter "name='$env:USERNAME' and domain='$env:COMPUTERNAME'").SID
And then replace the SID in this line
<UserId>S-1-5-21-1234567890-12345678-123456789-1001</UserId>
with your own.
- Bleeping_ComputerSep 05, 2023Copper Contributor
Hi HotCakeX,
Many thanks for your help!
I've changed line 5 to my Win10 local login id
<Author>vervlogen</Author>
Then, I changed line 16 to the SID for that account, which I found via PowerShell. There were other SID's for Administrator, etc. but I used the SID which corresponds to the User Account that I used for Author. My User Account does have Admin privileges.
I haven't changed anything else. I haven't changed <Principal id="Author"> to contain my User ID as Author or any of the other Author's, yet I'm still getting an error message.
"An error has occurred for task Win10 VPN Disconnect. Error Message. One or more of he specified arguments are not valid"
Any ideas of what might be wrong?
Many thanks again,
Bleep