Wednesday 22 April 2015

How to check if a remote server is reachable

You can store the servers in $Servers variable

_________________________________________________________________

$Servers = "Server1","Server2","Server3"

Foreach ($Server in $Servers)
{

  $StatusPC = Test-Connection -Cn $Server -BufferSize 16 -Count 1 -ea 0 -quiet

      if ($StatusPC -eq $True)
       {    
       Write-host
       Write-host "$Server is reachable" -foregroundcolor Green -backgroundcolor black
       }

       else
       {
        Write-host
        Write-host "$Server is not reachable" -foregroundcolor Red -backgroundcolor black
       }

}

       Write-host


_________________________________________________________________

Note: If the servers are reachable you will be able to see the output in Green if not then you will see Red 


Alternate One-Liner Script:
get-content servers.txt | % {new-object psobject -property 
@{ComputerName=$_; Reachable=(test-connection -computername $_ -quiet -count 1)} }  
| ft -AutoSize

No comments:

Post a Comment