Solved

Powershell Ping script

Posted on 2010-09-22
6
1,677 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 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