【发布时间】:2019-02-04 06:53:43
【问题描述】:
我有这个脚本:
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
}
它产生这个输出:
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
我们需要它没有空行:
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
我正在尝试来自 this 和 this 问题的解决方案,但没有任何帮助。
尤其是| ForEach-Object { $_.Trim() } 解决方案会产生此错误消息:
...不包含名为“Trim”的方法。
PS版:
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
【问题讨论】:
-
由于您使用的是 PowerShell,请尝试更改您的脚本以使用 PowerShell 命令 Test-NetConnection 而不是 ping。它将返回一个可用的 PowerShell 对象。
标签: powershell output