如何从Ping结果中读取特定值

时间:2016-09-15 08:22:44

标签: regex powershell networking ping

我正在尝试从ping结果中读取值,就像我想使用正则表达式将Received value读取为4或Lost值为0。

Ping statistics for 74.125.200.94:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 63ms, Maximum = 64ms, Average = 63ms

我正在尝试以下,但没有去,任何帮助?

$test = ping google.co.in
$test -match "^Average = \((\d+)\)$"

1 个答案:

答案 0 :(得分:1)

只需使用[regex]::Match来获取信息:

$test = ping google.co.in

$match = [regex]::Match($test, 'Received = (\d+), Lost = (\d+)')
$received = $match.Groups[1].Value
$lost = $match.Groups[2].Value