Saturday, 8 September 2018

PingTester++ - Ping Testing Multiple Lists of DNS Names/IP Addresses with PowerShell

A slight modification of a tool I published back in April 2015 - Multiple Pings Testing Tool. The main change is that it autosizes the output Windows, and opens a new PowerShell window running PingTester++ for you to supply another list of DNS Names / IP Addresses to ping test. The screenshot below gives an example.

Image: PingTester++

The variable $ToolPath will need to be set correctly for your environment.

The Script

Copy and paste into a text editor and save as say pingTester++.ps1 then run in PowerShell from the save directory as .\pingTester++.ps1


####################
# PingTester++.ps1 #
####################

[String]$ToolPath = "H:\MY TOOLS"
[String]$ToolName = "pingTester++.ps1"

[Int16]$i = 1
[Int16]$MaxWidth = 0
[System.Array]$targets = @()

"Enter IP Addresses/Hostnames of Targets (press enter to terminate entry)"    
[String]$answer = $TRUE
While($answer){
  $answer = Read-Host "$i"
  If($answer){
    $targets += $answer
    If($answer.length -gt $MaxWidth){
      $MaxWidth = $answer.length
    }
  }
  If(($answer -eq "") -and ($i -eq 1)){
    "No input - exiting!"; Sleep 5; EXIT
  }
  $i++
}

$MaxWidth += 6
$Window = (Get-Host).UI.RawUI

$resize = $Window.WindowSize
$resize.Height = $targets.count + 2
$resize.Width = $MaxWidth + 2
$Window.WindowSize = $resize

$resize = $Window.BufferSize
$resize.Height = 9999
$resize.Width = $MaxWidth + 2
$Window.BufferSize = $resize

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

Function Wr{Write-Host $Args[0] -BackgroundColor $Args[1] -ForegroundColor $Args[2]}

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"){
      Wr ($Display.PadRight($MaxWidth)) GREEN BLACK
    }elseif($status[$i] -eq "DOWN"){
      Wr ($Display.PadRight($MaxWidth)) RED WHITE
    }else{
      Wr ($Display.PadRight($MaxWidth)) GRAY 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++
  }
}

Start PowerShell.EXE ".\$ToolName" -WorkingDirectory $ToolPath
While ($true){GetResult}

No comments:

Post a Comment