Have you ever been in a situation where you needed to ping a server? Most everyone that has used a computer has done this at some point. As a System Administrator, I use this utility on a daily basis. Just fire up a command prompt or PowerShell and type, “ping MYSERVER01” and press enter. Easy peasy, right?

What if you needed to ping a long list of hundreds or thousands of servers? What if you also wanted that results to be put into a text file or csv file? I was put in this situation this week and instead of manually pinging thousands of servers (because we all know that would be SO much fun!), I made a custom script that will do it for me. Here is how I did it. Hopefully other sysadmins out there find this useful.

I had a list of servers in XLS format that I exported from System Center Configuration Manager 2012, it looked something like this, except they were different names and I had over a thousand of them:

image

Next, I added in some fields that would aid me in converting this into a text editor to make it scriptable. On each row, I added a ping before the server name and Parameters afterwards. Now it looked like this:

image

Next, I selected the contents of all these cells, copied to my clipboard, opened Notepad, and pasted the data in. It looked like this:

image

Next, I selected the space between ping and the server name, and copied this to the clipboard.

image

Then I clicked “Edit” and then selected “Replace”, in the “Find What” field I pasted the copied space, in the “Replace With” field, I simply pressed spacebar one time. Then pressed Replace All.

image

Now I need to set the parameters for each ping. I wanted my script to attempt to ping each server in the list twice and pipe the output to a text file in this location “c:\temp\IP_Results.txt”. This would be accomplished by Replacing “Parameters” with “-n 2 >> “c:\temp\IP_Results.txt” “ and then pressing Replace All.

image

Now I have my text exactly the way I need it, I even added a message at the end as a reminder of where the data will be:

image

Now I just needed to save the file as a PS1 and run it. I saved it as “Gabe_Test_Ping.ps1” to my desktop, launched PowerShell, and away it goes. In the below screenshot, noticed it processed the entire script and wrote out my Write-Host entry at the very end.

image

And here is my text file, these servers don’t exist so all of the pings failed:

image

Note – To launch a PowerShell script in your current working directory, you have to use “.\” in front of your script path.

Note2 – Also if you are unable to execute the script, check your Execution Policy settings. For information about setting this policy issue a “get-help Set-ExecutionPolicy –detailed” from PowerShell. Below are your options, in my lab I use Unrestricted.

image

I am very much a “scripting” novice, but I made the data and tools work for me. Instead of pinging over a thousand servers one by one, I can initiate a simple script and be much more productive with my time. With PowerShell you can work smarter, not harder! Would you have done this differently? Do you have any other administrative tasks you’ve automated in your enterprise? Let me know in the comments section.

Scripting How To: Ping Multiple Servers and Export Results
  • like for my env this “formula” works –> ping servename -n 2 >> IP_Results.txt.
    with your i had errors

    Cheers

    • Gabriel

      Thanks Kamil, I’ll go back and look at this and adjust as necessary. Appreciate the feedback.

  • Frank Analysis

    Thanks, this helped me out a lot. I was looking for a way to ping a bunch of VPN servers on Windows, and this did the trick. Cheers :)

    • Gabe

      Very cool Frank! Really enjoy getting this feedback.

      Gabe

%d bloggers like this: