Saturday, September 13, 2014

Capture multiple IP Latency or drops results with powershell Test-Connection - Part 2


Capture ping Latency or drops results with powershell - Part1 

In my earlier blog I used ping tool to capture Latency and Request time out details in CSV file. Here I have used native powershell Test-Connection cmdlet. One of the flexibility I am getting with Test-connection is ping Multiple IP or Computername.

How you can run this script?
I generally use Powershell ISE for writing script, launch it from start menu.


You might end up with error if it is blocked to run scripts (This is by default behavior). You can check the status using Get-ExecutionPolicy cmdlet. And modify policy to run script using Set-ExecutionPolicy cmdlet (Make sure you have opened powershell Run as administrator).


It ask for confirmation of registry entries.

 


Once the Green play button or F5 key pressed script will run and it will get loaded in the memor.

Now to execute type and press enter, make the necessary changes as per your requirement.
Ping-Latency -ComputerName 255.255.255.255, MyRemote, 192.168.33.40 -Latency 100 -Hours 1 -File c:\temp\CapturedData.csv

Ping-Latency: is cmdlet
-Computername: Mention more than 1 IP address (255.255.255.255, MyRemote, 192.168.33.40)
-Latency: Above what latency you want to capture data.
-Hours: For how many hours you want to run the cmdlet.
-File: CSV file location


Once it completed check the CSV file.

You can also just check typing Ping-Latency and press enter.

 
 #####################################  
 ## http://kunaludapi.blogspot.com  
 ## Version: 1  
 ## Tested this script on  
 ##  1) Powershell v3  
 ##  2) Windows 7
 ##
 #####################################  
function Ping-Latency {  
   <#  
   .SYNOPSIS  
   PowerShell function to get network Latency details  
   .DESCRIPTION  
   Includes parameters to monitor server and latecy, and capture data in file.  
   .EXAMPLE  
   Ping-Latency -ComputerName 127.0.0.1, MyRemote, 192.168.33.40 -Latency 100 -Hours 1 -File c:\temp\CapturedData.csv  
   #>   
   [CmdletBinding()]  
     param(  
     [Parameter(Mandatory=$False, ValueFromPipeline=$true,  
           HelpMessage="Enter one or more computer names separated by commas.")]  
     [alias("Computer","C")]  
     [string[]]$ComputerNames = $env:COMPUTERNAME,  
     [Parameter(Mandatory=$False)]  
     [alias("Latency","L")]  
     [string]$LatencytoMonitor = 100,  
     [Parameter(Mandatory=$False)]  
     [alias("Hour","H")]  
     [string]$Hours = 1,  
     [Parameter(Mandatory=$False)]  
     [alias("File","F")]  
     [string]$filename = "$env:USERPROFILE\Desktop"  
     )  
   Begin {  
     $time = $(Get-Date).AddMinutes($Hours)  
     $futuretime = "{0}:{1}" -f $time.Hour, $time.Minute  
   }  
   Process {  
     do  
     {  
       foreach ($Computer in $ComputerNames) {  
         $PingStatus = Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue   
         if ($PingStatus -eq $null) {  
           $TestResult = "" | Select-Object Hostname, IPAddress, "Time-ms", DateTime  
           $TestResult.Hostname = $computer  
           $TestResult.IpAddress = "NotReachable"  
           $TestResult."Time-ms" = "RequestTimeOut"  
           $TestResult.DateTime = Get-Date  
           $TestResult | Export-Csv -Path $filename -Append -NoTypeInformation  
         }  
         elseif ($Pingstatus.ResponseTime -gt $LatencytoMonitor) {  
           $TestResult = "" | Select-Object Hostname, IPAddress, "Time-ms", DateTime  
           $TestResult.Hostname = $computer  
           $TestResult.IpAddress = $pingstatus.IPV4Address  
           $TestResult."Time-ms" = $pingstatus.ResponseTime  
           $TestResult.DateTime = Get-Date  
           $TestResult | Export-Csv -Path $filename -Append -NoTypeInformation  
         }  
         else {  
           #Write-Host "Ping response time to $computer is normal $($pingstatus.ResponseTime)"  
         }  
         $RealTime = Get-Date  
         $currenttime = "{0}:{1}" -f $RealTime.Hour, $RealTime.Minute  
       }  
     }  
     while ($CurrentTime -ne $futuretime)  
   }  
   End {}  
 }  

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

Keywords: capture ping request timed out and high latency
Post a Comment