Tuesday, 14 April 2015

Multiple Pings Testing Tool

Introduction

This tool is designed so you can test ping response to multiple addresses, in a clear and compact display. An example of the output is below, in which the tool is being run against 7 IP addresses (of which 3 are down). The tool simply refreshes the display after every test.

Image: PingTestingTool.ps1
Note: The asterisk indicates which address is currently being tested. When addresses are up, this asterisk moves very quickly. If all IP addresses are up, sometimes the asterisk moves so quickly that it stays still!

The Script

Save the below as say PingTestingTool.ps1, and right-click the file and select “Run with PowerShell” to run, or run from PowerShell as .\PingTestingTool.ps1. Alternatively you can provide a PingTestingTool.ini file with the list of addresses to be pinged.

#######################
# PingTestingTool.ps1 #
#######################

Function ProcessArray {
  Param([System.Array]$ArrayToProcess)
  [System.Array]$ProcessedArray = @()
  Foreach ($ArrayLine in $ArrayToProcess){
    If( ($ArrayLine -ne "") -and ($ArrayLine.substring(0,1) -ne "#") ){ $ProcessedArray += $ArrayLine }
  }
  , $ProcessedArray
}

[Int16]$i = 1
[System.Array]$targets = @()
If( !(Test-Path PingTestingTool.ini) ){
  "Enter IP Addresses/Hostnames of Targets (press enter to terminate entry)"     
  [String]$answer = $True 
  While ($answer -ne ""){
    $answer = Read-Host "$i"
    If(  $answer -ne "" ){ $targets += $answer}
    If( ($answer -eq "") -and ($i -eq 1) ){ "No input - exiting!"; Sleep 5; EXIT }
    $i++
  }
} else {
  $targets = Get-Content PingTestingTool.ini
  $targets = ProcessArray $targets
  If(!$targets){ "Empty file - exiting!"; Sleep 5; EXIT }
}

[System.Array]$status = @()
$targets | Foreach { $status += "Unitialized" }

Function PrintStatus{
  Param([Int16]$pointer)
  If ($pointer -eq ($targets.Count - 1) ){ $pointer = 0}
  else {                                   $pointer ++ }     
  cls
  $i = 0
  $targets | Foreach {
    If ($pointer -eq $i){ $Display = " * $_ " }
    else {                $Display = "   $_ " }
    If ($status[$i] -eq "UP"      ){ Write-Host ($Display.PadRight(50)) -BackgroundColor Green -ForegroundColor Black }
    elseif ($status[$i] -eq "DOWN"){ Write-Host ($Display.PadRight(50)) -BackgroundColor Red   -ForegroundColor White }
    else                           { Write-Host ($Display.PadRight(50)) -BackgroundColor Gray  -ForegroundColor Black }
    $i ++
  }
}

Function GetResult{
  [Int16]$j = 0
  $targets | Foreach{
    [Boolean]$Result = Test-Connection -ComputerName $_ -Count 1 -Quiet
    If ($Result){ $status[$j] = "UP" }
    If(!$Result){ $status[$j] = "DOWN" }
    PrintStatus $j
    $j++
  }
}

While ($true){ GetResult }

No comments:

Post a Comment