Solved

Powershell Ping script

Posted on 2010-09-22
6
1,655 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
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

828 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