1

I'm trying to create simple "ping" job using PowerShell. And I want to do it in "pipeline" way. Though it looks like Where-Object receives strings, not objects of TestNetConnectionResult class. Could you please explain how to filter out the results of Test-NetConnection where ping was successful?

Get-Content .\adresses.txt | Test-NetConnection | Where-Object { $_.PingSucceeded } | Write-Output
derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • 1
    "Though it looks like Where-Object receives strings" - what makes you believe that? – Mathias R. Jessen Feb 12 '21 at 13:40
  • [1] the final pipeline stage does NOTHING. i would remove it. [*grin*] [2] when i test with valid & invalid ipv4 addresses ... i get the objects from the valid ones and a `WARNING:` stream message for the invalid ones. ///// so ... what - exactly - are you getting with some thing like >>> `@('127.0.0.1', '10.0.0.1', 'LocalHost')` <<< instead of your `Get-Content` call? – Lee_Dailey Feb 13 '21 at 03:40

4 Answers4

0
function megaPing {
    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipeline, Mandatory, Position = 0)]
        [string]$serverName
    )
    process {
        , [PSCustomObject]@{
            serverName    = $serverName
            PingSucceeded = (Test-NetConnection $serverName).PingSucceeded
        }
    }
}

"127.0.0.1", "127.0.0.2" | megaPing 

You need use pipe lines

0

The filter is working correctly but you need a final ForEach-Object for processing results in the pipeline.

Get-Content .\adresses.txt `
    | Test-NetConnection `
    | Where-Object { $_.PingSucceeded } `
    | ForEach-Object { Write-Host "SUCCEDED: $($_.ComputerName)" } 

Note: if you also want to silent warnings from Test-NetConnection please check this question.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
0

Here's another approach:

Get-Content -Path '.\adresses.txt' | Test-NetConnection | 
    Select-Object @{Name = 'Address'; Expression = {$_.ComputerName}},
                  @{Name = 'PingSucceeded'; Expression = {$_.PingSucceeded}}

If you don't want the Progress bar, do this:

$oldProgress = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'

Get-Content -Path '.\adresses.txt' | Test-NetConnection | 
    Select-Object @{Name = 'Address'; Expression = {$_.ComputerName}},
                  @{Name = 'PingSucceeded'; Expression = {$_.PingSucceeded}}

$ProgressPreference = $oldProgress

Result will look something like

Address      PingSucceeded
---------    -------------
127.0.0.1             True
10.10.2.153          False
Theo
  • 57,719
  • 8
  • 24
  • 41
0

Ignore warnings? Warnings wouldn't put objects in the pipeline anyway.

echo yahoo.com microsoft.com | set-content addresses.txt
Get-Content addresses.txt | Test-NetConnection -WarningAction SilentlyContinue | 
  Where PingSucceeded


ComputerName           : yahoo.com
RemoteAddress          : 74.6.231.20
InterfaceAlias         : Wi-Fi
SourceAddress          : 192.168.1.114
PingSucceeded          : True
PingReplyDetails (RTT) : 61 ms
js2010
  • 23,033
  • 6
  • 64
  • 66