Solved

Powershell Ping script

Posted on 2010-09-22
6
1,642 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

708 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

15 Experts available now in Live!

Get 1:1 Help Now