Wednesday, September 10, 2014

Capture ping Latency or drops results with powershell - Part1


Powershell Ping multiple IP addresses
Capture ping Latency or drops results with powershell - Part1
Capture multiple IP Latency or drops results with powershell Test-Connection - Part 2
Schedule PowerShell (.PS1 file) script in Windows Task Scheduler - Part 3
 
Many times users reported me that they are facing slowness and lagging on the terminal servers, after analysis I didn't find any issue with CPU or Memory but sometimes Network slowness. Network is fluctuating and latency in MS has been increased. (This help when opening case with ISP vendor or contacting network team.)

To know at what time this is happening I use this script to capture the time when Latency increased above the bench-marked one. It will also store if drops has been increased.

Once you run this script, it will ask for IPaddress or ComputerName. On the second line When should it captures the ping results (if latency increases equal or more than 80, it will keep storing data in CSV file). And on the final line the path for csv file to store data.

In this Powershell script i have used Ping tool to capture in my next post I will capture same data using other Powershell command.
 ######################################################################  
 $computername = Read-Host "Ipaddress or Computername"  
 $LatencytoMonitor = Read-Host "Latency to monitor"  
 #$filename = "C:\temp\PingResult-$computername.csv"  
 $filename = Read-Host "Type path ie C:\temp\ComputerNamePingResult.csv"  
 ######################################################################  
 $x = $true  
 do  
 {  
   $Ping = Ping -n 1 $computername | Select-String -Pattern "Request", "Reply from"  
   $Pingstatus = $Ping[0]  
   $IP = $($Pingstatus -split("reply from ") -split (":"))[1]  
   if ($Pingstatus -match "Request timed out") {  
     #$Pingstatus  
     $TestResult = "" | Select-Object IPAddress, Latencyms, DateTime  
     $TestResult.IpAddress = $computername  
     $TestResult.Latencyms = "NotReachable"  
     $TestResult.DateTime = Get-Date  
     $TestResult | Export-Csv -Path $filename -Append -NoTypeInformation  
   }    
   elseif ($PingStatus -match "Reply from $IP") {  
     if (-not (Get-Variable Latencyms)) {  
       Remove-Variable -Name Latencyms -Force  
     }  
     $Latencyms = $Pingstatus -split ("time")  
     $Latencyms = $Latencyms[1].Substring(1)  
     $Latencyms = $Latencyms.Split("ms")[0]  
     if ([Int]$Latencyms -ge $LatencytoMonitor) {  
       #$Pingstatus  
       $TestResult = "" | Select-Object IPAddress, Latencyms, DateTime  
       $TestResult.IPAddress = $computername  
       $TestResult.Latencyms = $Latencyms  
       $TestResult.DateTime = Get-Date  
       $TestResult | Export-Csv -Path $filename -Append -NoTypeInformation  
     }  
       <#else {  
         "DO nothing"  
       }#>  
   }  
   else {  
     #$Pingstatus  
     $TestResult = "" | Select-Object IPAddress, Latencyms, DateTime  
     $TestResult.IPAddress = $computername  
     $TestResult.Latencyms = "GenericFailure"  
     $TestResult.DateTime = Get-Date  
     $TestResult | Export-Csv -Path $filename -Append -NoTypeInformation  
   }  
 }  
 while ($x -eq $true)  
 ###############################################################################  
 #Please test it at your own risk.  
 http://kunaludapi.blogspot.com   
 Keywords: capture ping request timed out and high latency

No comments: