Solved

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

Posted on 2015-02-06
12
4,928 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
  • 7
  • 5
12 Comments
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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

0
 

Author Comment

by:tobe1424
Comment Utility
Thanks much! Let me give that a shot
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
Comment Utility
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
 

Author Comment

by:tobe1424
Comment Utility
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
Comment Utility
You can remove the  parameter -Count 2 and try..
0
 

Author Comment

by:tobe1424
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:tobe1424
Comment Utility
let me try
0
 

Author Comment

by:tobe1424
Comment Utility
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
Comment Utility
What is the result for
Test-Connection 172.24.20.36 

Open in new window

0
 

Author Comment

by:tobe1424
Comment Utility
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
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert MSI to MSM 1 24
VBS Script not working correctly. 1 14
Custom attributes in Exchange 8 34
Powershell 16 30
This article will show, step by step, how to integrate R code into a R Sweave document
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now