Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8417335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:00:50+00:00 2026-06-10T02:00:50+00:00

I have script: $servers = server01, s02, s03 foreach ($server in $servers) { $server

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:00:52+00:00Added an answer on June 10, 2026 at 2:00 am

    You should not be modifying the value of $server within the foreach loop. Declare a new variable (e.g. $result). Also, Ping.Send takes 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.

    $servers = "server1", "server2"
    
    foreach ($server in $servers) {
       & {
           trap [System.Net.NetworkInformation.PingException] { continue; }
           $result = (New-Object System.Net.NetworkInformation.Ping).send($server)
           if ($result.Status -eq "Success") {
             Write-Host "$server is OK"
           }
           else {
             Write-Host "$server is NOT OK"
           }
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jQuery script that polls my server for new data, but it
I have a script that connects to a list of servers in a foreach
I have a server script that I need to pass data to from the
I have a Bash script on server A that finds the logged in SSH
I have script that reads remote file content and writes it to local server.
I have a script that connects to SQL Server 2005 default instance. But I'm
I have a script that listens to a jabber server and responds accordingly. Though
I have a PHP script on a server <?php // retrieve POST vars $comment
I have VB Script which is connected with the my database (SQL Server 2008),
I am using SQL Server 2008 R2, I have a script to update the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.