Description
Sometimes it is necessary to do a ping test to find out if there are dropped packets. It is not practical to keep watch on the continuous ping test overnight.
I wrote a little PowerShell script to make use of the Test-Connection cmdlet. It writes to a log file the change in network status. I didn't want it to record every ping responses. Basically it will record the date and time initially of the network status of a host. Then when there's a change in status, it will record the date and time and the state of the host i.e. UP or DOWN.
From the logs, you can see if there had been dropped packets by looking at the change in status.
The Code is below :
Sometimes it is necessary to do a ping test to find out if there are dropped packets. It is not practical to keep watch on the continuous ping test overnight.
I wrote a little PowerShell script to make use of the Test-Connection cmdlet. It writes to a log file the change in network status. I didn't want it to record every ping responses. Basically it will record the date and time initially of the network status of a host. Then when there's a change in status, it will record the date and time and the state of the host i.e. UP or DOWN.
From the logs, you can see if there had been dropped packets by looking at the change in status.
The Code is below :
## Get current date and time
$currentdate=get-date
$count=1
$myHost="10.0.1.1"
$maximumcount=10
$networkstatus=0 # 0 = up and 1 = down
$outputfile="C:\PowerShell\ pinglog.txt"
# Do the initial ping test
$currentdate=get-date
$pingResponse=Test-Connection -ComputerName $myHost -Quiet
if ($pingResponse -eq $true) {
$response= $currentdate.toString() + " - " + "Beginning to ping. Network status changed to UP."
write-output $response >> $outputfile
$networkstatus=0
}
else {
$networkstatus=1;
$response = $currentdate.toString() + " - " + "Beginning to ping. Network status changed to DOWN."
write-output $response >> $outputfile ;
}
write-output "Currently running ping operation.... Press Ctrl-C to end.."
do {
$currentdate=get-date
$pingResponse=Test-Connection -ComputerName $myHost -Quiet
if ($pingResponse -eq $false)
{
if ($networkstatus -eq 0)
{
$networkstatus=1
$response = $currentdate.toString() + " - " + "Network status changed to DOWN"
write-output $response >> $outputfile
};
}
else
{
if ($networkstatus -eq 1)
{
$networkstatus=0
$response = $currentdate.toString() + " - " + "Network status changed to UP."
write-output $response >> $outputfile
};
};
## Remark the following out to make it run forever
## $count++;
}
while ($count -LE $maximumcount)
Comments
Post a Comment