Log When Connectivity Drops To An IP address – PowerShell

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.

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.


#Set the IP or Hostname you want to test
$target = ""
#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
 trap {$false; continue}
 $timeout = 1000
 $object = New-Object system.Net.NetworkInformation.Ping
 (($object.Send($ip, $timeout)).Status -eq 'Success')
 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.