3

I have this script:

for ( ; $true ; )
{
    Write-Host ""
    Get-Date -UFormat "%Y-%m-%d %H:%M:%S"
    ping -n 1 10.10.50.203 | Select-String -SimpleMatch "Pinging" -Context 1,2
    ping -n 1 10.10.50.201 | Select-String -SimpleMatch "Pinging" -Context 1,2
    timeout 5 > null
}

It produces this output:

2018-08-29 14:40:49


> Pinging 10.10.50.203 with 32 bytes of data:
  Request timed out.


> Pinging 10.10.50.201 with 32 bytes of data:
  Reply from 10.10.50.201: bytes=32 time=58ms TTL=126

2018-08-29 14:40:54

> Pinging 10.10.50.203 with 32 bytes of data:
  Request timed out.


> Pinging 10.10.50.201 with 32 bytes of data:
  Reply from 10.10.50.201: bytes=32 time=58ms TTL=126

We need it without the empty lines:

2018-08-29 14:40:49
> Pinging 10.10.50.203 with 32 bytes of data:
  Request timed out.
> Pinging 10.10.50.201 with 32 bytes of data:
  Reply from 10.10.50.201: bytes=32 time=58ms TTL=126

2018-08-29 14:40:54
> Pinging 10.10.50.203 with 32 bytes of data:
  Request timed out.
> Pinging 10.10.50.201 with 32 bytes of data:
  Reply from 10.10.50.201: bytes=32 time=58ms TTL=126

I was trying solutions from this and this questions but nothing helps.

Especially the | ForEach-Object { $_.Trim() } solution produces this error message:

... does not contain a method named 'Trim'.

PS Version:

PS C:\WINDOWS\system32> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17134.165
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.165
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
Carsten
  • 325
  • 2
  • 12
  • 4
    Since you're using PowerShell, try to alter your script to use the PowerShell command Test-NetConnection instead of ping. It will return an usable PowerShell object. – TobyU Aug 29 '18 at 13:09

3 Answers3

6

This works fine.

for ( ; $true ; )
{
    Write-Host ""
    Get-Date -UFormat "%Y-%m-%d %H:%M:%S"
    (ping -n 1 www.google.com | Select-String -SimpleMatch "Pinging" -Context 1,2 | Out-String).Trim()
    (ping -n 1 www.google.co.in | Select-String -SimpleMatch "Pinging" -Context 1,2 | Out-String).Trim()
    timeout 5 > null
}

Output :

2018-08-29 18:42:10
> Pinging www.google.com [216.58.197.36] with 32 bytes of data:
  Reply from 216.58.197.36: bytes=32 time=21ms TTL=53
> Pinging www.google.co.in [216.58.197.35] with 32 bytes of data:
  Reply from 216.58.197.35: bytes=32 time=15ms TTL=57
HariHaran
  • 3,642
  • 2
  • 16
  • 33
0

Especially the | ForEach-Object { $_.Trim() } solution produces this error message:

... does not contain a method named 'Trim'.

I'm guessing that was because you didn't use

out-string | 

before the ForEach?

i.e.

*some-command* | out-string | ForEach-Object { $_.Trim() }
ultramegaok
  • 131
  • 5
0

You can also use -replace "`n",'' to remove empty lines. This can be particularly useful when wanting to keep whitespace in e.g. indented lines that Trim() method otherwise will remove. Often there is no need for Out-String eiher.

In this example the code would be:

for ( ; $true ; )
{
    Write-Host ""
    Get-Date -UFormat "%Y-%m-%d %H:%M:%S"
    (ping -n 1 10.10.50.203 | Select-String -SimpleMatch "Pinging" -Context 1,2) -replace "`n",''
    (ping -n 1 10.10.50.201 | Select-String -SimpleMatch "Pinging" -Context 1,2) -replace "`n",''
    timeout 5 > $null
}
loxia_01
  • 13
  • 3