I have script:
$servers = "server01", "s02", "s03"
foreach ($server in $servers) {
$server = (New-Object System.Net.NetworkInformation.Ping).send($servers)
if ($server.Status -eq "Success") {
Write-Host "$server is OK"
}
}
Error message:
An exception occured during a Ping request.
I need to ping each server in $servers array and display status. I think, that Foreach statement is not properly used, but I’m unable to find out where is the problem. Thank you for your advice
You should not be modifying the value of
$serverwithin the foreach loop. Declare a new variable (e.g.$result). Also,Ping.Sendtakes the individual server name, not an array of server names as an argument. The following code should work.Finally, you will need to trap the PingException that will be thrown if the host is unreachable, or your script will print out a big red error along with the expected results.