Log When Connectivity Drops To An IP address - PowerShell

How To Guides Oct 05, 2020

There have been many times when I have been investigating a server or device dropping and then coming back online. Here is a useful PowerShell script that updates a txt file each time a device drops.

The PowerShell script records the date and time of each drop and can be very easily configured using the variables at the top of the script.

<#  
Script to log drop in connectivity
Checks every 10 seconds. If a drop is detected it checks every 60 seconds until it comes back online.
#>

#Variables

#Set the IP or Hostname you want to test
$target = "8.8.8.8"
#Set the location of the log file
$logfile = "C:\result.txt"
#set how often to check connectivity (in seconds)
$checktime = 10
#set how long before chacking after a drop in connectivity (in seconds)
$rechecktime = 60

#no changes required past here
function Test-Ping
 {
 param($ip)
 trap {$false; continue}
 $timeout = 1000
 $object = New-Object system.Net.NetworkInformation.Ping
 (($object.Send($ip, $timeout)).Status -eq 'Success')
 }
 
$killswitch=1
 Write-Host "Running a network test. **PLEASE DO NOT CLOSE THIS WINDOW**" -Fo Red
while ($killswitch -ne 0) {
 
If (!(Test-Ping $target)) {
 Write-output 'No connection at: ' $(Get-Date -format "dd-MM-yyyy @ hh:mm:ss") | out-file -append $logfile -force
 Start-Sleep $rechecktime
 }
 Else {
 Start-Sleep $checktime
 }
}

Hope you find this useful!

Tags

Jonathan Blench

I enjoy all things geeky and techie. Work in IT with a focus on Cloud and Data Centre.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.