life runs on code

bending technology to do your bidding

Automatically Reconnect ExpressVPN via Expresso CLI

I run the ExpressVPN Windows client on a virtual machine and I wanted it to reconnect automatically when the connection was dropped.  I was able to accomplish this using the Expresso CLI, a couple of lines of PowerShell and a Windows scheduled task. Expresso CLI allows you to orchestrate the following operations of the ExpressVPN client via the command line: 

  • Check the status of the ExpressVPN Connection
  • Connect to an ExpressVPN location
  • Disconnect from ExpressVPN
  • List all available ExpressVPN locations

Within the ExpressVPN Windows client, I enabled the Network Lock (kill switch) feature of ExpressVPN to Stop all internet traffic if the VPN disconnects unexpectedly.  For convenience, I extracted the expresso.exe command to a folded on my drive which was included in the PATH environment variable.

Check-VPNConnection.ps1

$output = & expresso.exe status
Write-host $output
if ($output -ieq "VPN not connected") { expresso connect 'UK - Wembley' }

Next, I configured a Windows Task to run Check-VPNCOnnection.ps1 whenever Event ID 10001 from the Microsoft-Windows-NetworkProfile/Operational EventLog with an Event Source of NetworkProfile fires. Event ID 1001 indicates that the Network has gone into a Disconnected state.

The Windows Task is configured as follows:

Log: Microsoft-Windows-NetworkProfile/Operational
Source: NetworkProfile
Event ID: 10001

For completeness, you could also additionally schedule the Windows Task to run on a periodic, repeating time basis.

Program/script: powershell.exe
Arguments: -NonInteractive -File "C:\Tools\scripts\check-vpnconnection.ps1"