Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell script to ping a list of IP address and output to file

Posted on 2015-02-06
12
Medium Priority
?
16,153 Views
Last Modified: 2015-02-06
Per the title, I need a simple script to ping a list of IP addresses. If it helps, I am pinging a list of printers. But I would like to be able to use this for other systems as well.

Any how, I have a text file which contains a list IP's. I simply want ping output to display whether the host is online or not ( icmp replies or timeouts)

many thanks
0
Comment
Question by:tobe1424
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40593847
Here is a sample code which will read a text file and display the server status on console..
GC C:\Temp\Server.txt | %{
	If (Test-Connection $_ -Quiet -Count 2){
	Write-Host "$_ is UP" -b Green
	}
	Else{
	Write-Host "$_ is Down" -b Red
	}
}

Open in new window

1
 

Author Comment

by:tobe1424
ID: 40593850
Thanks much! Let me give that a shot
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 2000 total points
ID: 40593856
With small modification, you can save result in to a file..
GC C:\Temp\Server.txt | %{
	If (Test-Connection $_ -Quiet -Count 2){
	"$_ is UP"
	}
	Else{
	"$_ is Down"
	}
} | Out-File C:\Temp\result.txt

Open in new window

0
Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

 

Author Comment

by:tobe1424
ID: 40593863
I like how the script operates...pretty cool. However, they appear to be "down" when in fact I can ping some of the addresses in my list

powershell output
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40593867
You can remove the  parameter -Count 2 and try..
0
 

Author Comment

by:tobe1424
ID: 40593869
This is the output from the same pc.

Pinging 172.24.20.36 with 32 bytes of data:
Reply from 172.24.20.36: bytes=32 time<1ms TTL=60
Reply from 172.24.20.36: bytes=32 time<1ms TTL=60
0
 

Author Comment

by:tobe1424
ID: 40593871
let me try
0
 

Author Comment

by:tobe1424
ID: 40593877
Thanks for prompt help. i removed the parameter..but it still shows red. They are printers, not servers. However, its just an IP we are talking about here so I am dumbfounded
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40593889
What is the result for
Test-Connection 172.24.20.36 

Open in new window

0
 

Author Comment

by:tobe1424
ID: 40593898
Seems to be working


Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)
------        -----------     -----------      -----------                              -----    --------
MYHOST 172.24.20.36    172.24.20.36     {}                                       32       0
MYHOST 172.24.20.36    172.24.20.36     {}                                       32       1
MYHOST 172.24.20.36    172.24.20.36     {}                                       32       0
MYHOST 172.24.20.36    172.24.20.36     {}                                       32       0
1
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 40593909
ok.. Probably your input file have trailing space for IP addresses. Change the second line to..
	If (Test-Connection $_.Trim() -Quiet -Count 2){

Open in new window

1
 

Author Comment

by:tobe1424
ID: 40593946
Yes this is a preview from my input file:

172.24.20.36
172.24.20.42
172.24.12.34
172.24.20.24
172.24.16.24
172.24.14.27
172.24.14.26
172.24.18.16
172.24.20.25
172.24.20.31
172.24.18.30
172.24.18.29
--


excellent! that worked

cheers
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question