Solved

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

Posted on 2015-02-06
12
11,589 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 500 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 500 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 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