Keep-alive VPN on Windows using PowerShell


Having to rely on Windows VPNs sometimes can be tricky, especially if the VPN tunnel times out.

We can use this script for monitoring the tunnel and reconnecting automatically whenever it goes down.

We are using the password for the VPN encrypted as seen on this post.

$ip = ""
$result = gwmi -query "SELECT * FROM Win32_PingStatus WHERE Address = $ip'"
$credentials = Get-Content c:vpnpassword.txt | ConvertTo-SecureString

if ($result.StatusCode -eq 0) {
  Write-Host "$ip is up."
  Write-Host "$ip is down."
  Write-Host "Disconnecting..."
  rasdial.exe "Branch Office" /DISCONNECT
  Write-Host "Connecting..."
  rasdial.exe "Branch Office" username $credentials


Leave a Reply

Your email address will not be published. Required fields are marked *