PowerShell script to monitor internet connection failover

PowerShell logo

In an infrastructure with two internet links I was asked to monitor the times when the connection switched from one link to the other.

In a full Windows environment I developed a PowerShell script to do that.

Let's see, in details, how it works…

Get the current public ip address with PowerShell

The goal here is to get the current public ip. To do so we will use external service as http://ifconfig.me.

Mozilla Firefox | http://ifconfig.me/ip address to get current ip

Great, a simple text with ip address : easy!

  • So, to get it from PowerShell we simply need to enter this command :
PS C:\ > (New-Object System.Net.WebClient).DownloadString('http://ifconfig.me/ip')
PowerShell | Get public ip address
  • A little more complicated, but not that much, we could also use json format with https://api.ipify.org website :
PS C:\ > ((New-Object System.Net.WebClient).DownloadString('https://api.ipify.org?format=json') | ConvertFrom-Json).ip
PowerShell | Get public ip address

PowerShell Script

Replace default_ip variable with your current IP.

###########################
# author : shebangthedolphins.net
# version : 1.0
# date : 2021.06.26
# role : monitor wan link, send email in case of failover
# other : Tested on Windows 2019 Server
# updates :
#       - 1.0 (2021/06) : First Version

#we set our default wan ip
$default_ip = '94.198.4.X'

#Function to send emails
Function Mail {
    param ([string]$emailbody, [string]$subject)

    $encoding = [System.Text.Encoding]::UTF8
    Send-MailMessage -Encoding $encoding -To check_wan@shebangthedolphins.net -Subject $subject -From check_wan@shebangthedolphins.net -smtpserver mx.shebangthedolphins.net -Body $emailbody #send email
    }

#We create a checkip_state.txt file whith 1 value. 1 value when wan ip is equal to $default_ip, 0 when wan ip is diffrent from $default_ip
if (!(Test-Path C:\Windows\Temp\checkip_state.txt -PathType Leaf)) { #if C:\Windows\Temp\checkip_state.txt has not been created
    Set-Content C:\Windows\Temp\checkip_state.txt '1' #then we create C:\Windows\Temp\checkip_state.txt with default value
    }

#get public ip address
$currentIP = (New-Object System.Net.WebClient).DownloadString('http://ifconfig.me/ip')
#$currentIP = ((New-Object System.Net.WebClient).DownloadString('https://api.ipify.org?format=json') | ConvertFrom-Json).ip

#If public ip is Null (in case of http://ifconfig.me/ip fails) or is equal to default public ip
If(($currentIP -eq $default_ip) -or ($currentIP -eq $Null)) {
    Write-Host "currently on default link"
    if ($(get-content C:\Windows\Temp\checkip_state.txt) -eq '0') { #if C:\Windows\Temp\checkip_state.txt value is equal to 0 it means we were previously on alternative link, this is a link change so we send a mail
        Mail "We are currently using default link with public IP : $currentIP" "Check Failover : link has changed"
        Set-Content C:\Windows\Temp\checkip_state.txt '1' #we set checkip_state.txt to 1 (which means default link)
    } 
 } else {
    Write-Host "currently on alternative link"
    if ($(get-content C:\Windows\Temp\checkip_state.txt) -eq '1') { #if C:\Windows\Temp\checkip_state.txt value is equal to 1 it means we were previously on default link, this is a link change so we send a mail
        Mail "We are currently using alternative link with public IP : $currentIP" "Check Failover : link has changed"
        Set-Content C:\Windows\Temp\checkip_state.txt '0' #we set checkip_state.txt to 0 (which means alternative link)
    }
}

Now follow this link to create a Windows task in order to run the script periodically.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :