RSS

Tag Archives: ping

PS: Continuous ping of a list of VMs

Generally, continuous pings are very easy, we can even do it from Windows command prompt. However, recently, I need a more generic script used to ping a list of VMs and color code status and it expanded from there to include other stuff I want to with the script.

Note that the script is used to ping a set of VMs in my environment that is why the input CSV has VMHost as a column. You can always change this to suit your needs.



Param (
  [Parameter(Mandatory=$true)]
  [ValidateScript({If(Test-Path $_){$true}<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>else{Throw "Invalid file given: $_"}})]
  [string]$inputfile,
  [int]$count=1,
  [int]$first=0,
  [int]$skip=0,
  [switch]$loop,
  [switch]$v
)

$ListOfVMs = import-csv $inputfile
if ($first -eq 0) {$first = $ListOfVMs.count}
$ListofVMs = $ListofVMs | select -first $first -skip $skip
do {

  Clear
  Write-Host "Checking Status ... "
  Write-Host " "

  foreach ($VM in $ListOfVMs) {
    $VMHost = $VM.VMHost
    $VMName = $VM.Name
    $IPAddress = $VM.IPAddress

    if ($v) {
       Write-Host "Pinging... $VMHost : $VMName with $IPAddress" -ForegroundColor yellow
       Test-Connection $IPAddress -EA SilentlyContinue
    }
    else {
       if (Test-Connection $IPAddress -count $count -quiet -EA SilentlyContinue) {
         Write-Host "$VMHost : $VMName with $IPAddress is UP" -foregroundcolor Green
       }
       else {
         Write-Host "$VMHost : $VMName with $IPAddress is DOWN" -foregroundcolor Red
       }

    }
  }

  Write-Host " "

  $Endloop=$false
  if ([console]::KeyAvailable) {
    $x = [System.Console]::ReadKey()
    if ($x.key -match "Q") {
      $Endloop=$true
	  #[System.Console]::Clear()
	  Write-Host "Keystroke Q detected, quiting loop"
    }
  }

  if ($loop -and !$Endloop) {
    Write-Host "Continuous ping enabled, press Q to end loop ... "
    Write-Host " "
	sleep 1
  }

} while($loop -and !$Endloop)

Write-Host " "

Advertisements
 
Leave a comment

Posted by on April 20, 2018 in powershell

 

Tags:

PS: One-liner to check IP range usage

Not as quick as command prompt, but you get more formatting options (if you want)

for ( $i=200; $i -ne 254 ; $i++)
 { $IPAddress = "10.164.144.$i" ; 
   $InUse = test-connection $IPAddress -Quiet -count 1 ; 
    Write-host $IPAddress`t$InUse }

With color coding

for ( $i=200; $i -ne 254 ; $i++)
 { $IPAddress = "10.164.144.$i" ; 
   $InUse = test-connection $IPAddress -Quiet -count 1 ; 
    Write-host $IPAddress -NoNewLine ;
    if ($InUse) {Write-Host `t$InUse -ForegroundColor green} 
     else {Write-host `t$Inuse -ForegroundColor Red}  
 }
 
Leave a comment

Posted by on April 17, 2018 in powershell

 

Tags: