How Do I Filter Out Unavailable Hosts When Pinging an IP Range?

0
16
Asked By CuriousCat92 On

I'm working with a script that pings a range of IP addresses from 10.1.1.1 to 10.1.1.254. The goal is to report the available hosts, but I'm running into an issue. Currently, the output shows "available" even for unreachable hosts. I want to include responses like "Reply from 10.1.1.55: bytes=32 time=3ms TTL=53" but exclude any lines that say "Destination host unreachable." How can I modify my script to filter those out? Here's what I have so far:

```powershell
$iprange = 1..254
Foreach ($ip in $iprange)
{
$computer = "10.1.1.$ip"
$status = Test-Connection $computer -count 1 -Quiet
if (!$status)
{
$computer + " - available"
}
}
```

2 Answers

Answered By SnarkyScriptMaster On

Honestly, none of those commands support the Quiet parameter! But you can use `Test-NetConnection -WarningAction SilentlyContinue -ErrorAction SilentlyContinue` and look at the `PingSucceeded` attribute to see if your pings are successful. Also, record the output and do what you need with it!

LogicGuru88 -

Actually, `Test-Connection` does have a -Quiet option, but it's listed under `-InformationLevel Quiet` in the manual. So it's okay to shorten it to just `-Quiet`.

Answered By TechSavant77 On

You might want to try using `Test-NetConnection -Quiet`. It can handle that filtering better. Just check out the [PowerShell documentation](https://learn.microsoft.com/en-us/powershell/module/nettcpip/test-netconnection?view=windowsserver2025-ps) for more details!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.