Link to home
Start Free TrialLog in
Avatar of OTNAdmin
OTNAdminFlag for Afghanistan

asked on

Powershell Ping script

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

Avatar of Jack van Deur
Jack van Deur
Flag of Netherlands image

Avatar of OTNAdmin

ASKER

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

-exclude
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
ASKER CERTIFIED SOLUTION
Avatar of wittyslogan
wittyslogan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks that worked