Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell Ping script

Posted on 2010-09-22
6
Medium Priority
?
1,687 Views
Last Modified: 2012-05-10
I want to create a powershell script that will ping a range of IP's then out put the results. Here is the challenge, I want to know if it is possible to only have the output file contain the IP address of the servers that are reachable.

Below is the script I am working with, right now it does not output any results:


function ping-ip
{ param($ip)
$ping = New-Object System.Net.NetworkInformation.Ping #new ping 
$Reply = $ping.send($ip)          #send ping   
if ($Reply.Status -eq "Success")        #ping status
{  Write-Host "IP reachable: " $ip -ForeGroundColor "Green" #succeeded
} 
else 
{
Write-Host "IP not reachable: " $ip -ForeGroundColor "Red" #not reacheble 
}
}
0..255 | foreach { $ip = "10.224.129.$_" ; (ping-ip $ip) }

Open in new window

0
Comment
Question by:OTNAdmin
[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
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:JP64
ID: 33734210
0
 

Author Comment

by:OTNAdmin
ID: 33734768
I was able to get it working by just modifying the script see below, I got it to output to a txt file by changing it to write-output.

Now I have one last component I need help with. when I am pinging the subnet is there a way to omit a certain ip address from the loop?
function ping-ip
{ param($ip)
$ping = New-Object System.Net.NetworkInformation.Ping #new ping 
$Reply = $ping.send($ip)          		#send ping   
if ($Reply.Status -eq "Success")        #ping status
{  Write-Output $ip # -ForeGroundColor "Green" #succeeded
} 
else 
{
#Write-Host "IP not reachable: " $ip -ForeGroundColor "Red" #not reacheble 
}
}
0..255 | foreach { $ip = "10.224.129.$_" ; (ping-ip $ip) }

Open in new window

0
 
LVL 7

Expert Comment

by:wittyslogan
ID: 33734802
-exclude
0
Technology Partners: 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:OTNAdmin
ID: 33735478
I am new to powershell can you assist in how I can add it to the following script? The IP i want to exclude is 10.224.129.103
0
 
LVL 7

Accepted Solution

by:
wittyslogan earned 2000 total points
ID: 33743170
Hello,
I am also new and on reflection you could do the following
0..102+104..255 | foreach { $ip = "10.224.129.$_" ; (ping-ip $ip }

basically this looks through 2 ranges 0-102 and then 104-256.  I think you can do this with as many ranges as you want.

0
 

Author Closing Comment

by:OTNAdmin
ID: 33743684
thanks that worked
0

Featured Post

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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)
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…

719 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