Skip to content

Instantly share code, notes, and snippets.

@megadix
Last active January 17, 2024 15:51
Show Gist options
  • Select an option

  • Save megadix/97477a42bb8c150f1e9ba7b4f4955600 to your computer and use it in GitHub Desktop.

Select an option

Save megadix/97477a42bb8c150f1e9ba7b4f4955600 to your computer and use it in GitHub Desktop.
VPN Heartbeat
<#
.SYNOPSIS
Keep a VPN connection alive by periodically issuing a GET
.DESCRIPTION
Keep a VPN connection alive by periodically issuing a GET on a specified URL and log status code.
.PARAMETER url
URL to GET.
.PARAMETER sleep
Interval (in seconds) between GET invocations.
.EXAMPLE
Run script on default url
vpn-heartbeat.ps1
.EXAMPLE
Run script on custom url and sleep interval
vpn-heartbeat.ps1 -url https://www.megadix.it/ -sleep 120
#>
param(
# url
[String]$url = "https://www.megadix.it/",
# sleep interval
[Int32]$sleep = 60
)
Write-Output "Heartbeat script"
Write-Output "Url : $url"
Write-Output "Sleep : $sleep s`n"
while ($true) {
Write-Output "Heartbeat: $url"
try {
$result = Invoke-WebRequest -Uri $url
if ($result.StatusCode -eq 200) {
Write-Output "...received: HTTP $($result.StatusCode)"
}
else {
# Log status code
Write-Error "...received: HTTP $($result.StatusCode)"
}
}
catch {
# Log error and continue
Write-Error "...an error occurred: $($_.Exception.Message)"
Write-Error "retrying in ${sleep} seconds..."
}
Start-Sleep $sleep
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment