Tuesday, November 15, 2011

Ping Multiple Servers using “PowerShell”

 

Hi,

Today I was looking for  a little PowerShell script which can Ping multiple servers and give a consolidated view on PowerShell console that which one is Live and which one is Dead.

Then a decide to write my own and finally wrote  it: ) and it is a only a 9 line script :)

You can copy and paste the script from below or you can download it from here : http://dl.dropbox.com/u/17858935/Ping_Multiple_Servers.zip 

let me explain the script :

$Servername: is a variable which contain a list  of comma separated Server name

in foreach script block we are selecting all servers in $ServerName variable and run Test-Connection cmdlets against them

in If Test-Connection evaluates that servers in Pinging that it writes “S$server is alive” otherwise it write “Server is dead”,

In test-connection Cmdlet we define that it should send only 2 Pings to the server using –count 2 switch parameter and used –Quiet Switch parameter so that is show the output in Boolean (true or false)

 

   1:  #### Provide the computer name in $computername variable
   2:   
   3:  $ServerName = "Dc-2","LocalHost","Server-2","Not-Exists","Fake-computer"
   4:   
   5:  ##### Script Starts Here ###### 
   6:   
   7:  foreach ($Server in $ServerName) {
   8:   
   9:          if (test-Connection -ComputerName $Server -Count 2 -Quiet ) { 
  10:          
  11:              write-Host "$Server is alive and Pinging " -ForegroundColor Green
  12:          
  13:                      } else
  14:                      
  15:                      { Write-Warning "$Server seems dead not pinging"
  16:              
  17:                      }            
  18:  }
  19:   
  20:  ########## end of script #######################



The output of the script will be like below link.


Ping


 


Download Link : http://dl.dropbox.com/u/17858935/Ping_Multiple_Servers.zip 


 


Thanks


aman dhally

14 comments:

  1. I was looking at the code here: http://exchangeserverpro.com/powershell-send-html-email, and I'm able to email based on that code. My question is, how do I email the output from your script Ping Multiple Servers using “PowerShell”?

    ReplyDelete
    Replies
    1. Hi David
      Please check your email, i sent you the updated version of this script which include the email facility usind Send-MailMessage cmdlet.

      or you can download the script from here :
      http://dl.dropbox.com/u/17858935/Ping_Multiple_Server_With_Email.zip

      thanks
      aman

      Delete
  2. I got the updated version. It's simple and beautiful! Thank you.

    ReplyDelete
    Replies
    1. You are welcome David :).. I glad that it helps you ...

      Thanks
      aman

      Delete
    2. Aman,
      Thank you for the script. I need some help modifying the script. I want the script to check a list of servers, and send one simple email that says everything is ok, I do not want it to list every server because the email is really long..Any Suggestions?
      Thanks,
      Jarrett

      Delete
    3. HI jarrnett, you are welcome..and i glad that u like the script..

      humm..can u email me so that we can discuss this.. currently i am not sure how to do it, but i think this should be possible.

      thanks
      aman

      Delete
  3. Great Script: Simple, Fast, Elegant
    Thank You

    ReplyDelete
    Replies
    1. You are Welcome Marcio..

      thanks
      aman

      Delete
    2. hi aman,

      i got email report successfully,

      but i need with color like offline pc in red and online pc in green color

      Delete
  4. Hello Aman,

    For some reason every time i run this script, a ps window appears and disappears right away. I'm I missing something? I get no output at all.

    ReplyDelete
  5. why cant I copy and paste the GUI output?

    ReplyDelete
  6. I changed the write-host and write-warning to write-output ( I also removed the color selections) and am trying to format to write-output in the "else" statement to save all the failed pings in a text file at c:\errors. Can you advise me of the syntax to do this? I only get the first failed ping in my text file.

    Write-output "Server$ " > C:\error.txt

    ReplyDelete
  7. Hi Aman Dhally , can you please explain how can i do a script to ping test 20 servers and get the output via emails , also can i do a ping test to my default gateways which is around 10 of them , please can you help me out . thanks a lot

    ReplyDelete
  8. Hi,

    Please share me the script, which can send for the server down from the array list

    regards,
    Vijay

    ReplyDelete

Note: Only a member of this blog may post a comment.