07/10/12

Powershell Script to PING without Exception Errors

I have tried a lots of scripts to use a simple ping script in Powershell, but the exeption error if a server isn´t available wasn´t the right solution for me. So I tried to  write a script that work with the status of a ping. But the problem here was that the status was “success” although the ping writes “The Host is unreachable”. So the Script don´t work correct.

Now I have written a scripte that can ping a list of server in a txt file without exeption errors in a invinite loop. Simple but Useful! 😉

 

$serverliste = get-content "C:\client.txt"

$i =1
While ($i -le 10) 
{
    foreach ($server in $serverliste) {
        if ( Test-Connection -ComputerName $server -Count 1 -ErrorAction SilentlyContinue ) {

            Write-Host "$server Ping is OK" -ForegroundColor Green
            sleep 1

        }
        else {

            Write-Host "$server Ping FAILED" -ForegroundColor Red

        }
    }

    Write-Host ""
}