$data = @("server1","server2","server3")
foreach ($server in $data) {
$results = (Test-Connection $server -count 1 | Measure-Object -Property ResponseTime -Average).Average
}
我试图弄清楚如何获取ForEach的输出并将结果存储到数组中,以便稍后我可以比较它们。我还想办法做到这一点。
答案 0 :(得分:1)
通过执行ForEach
,您只需测量标量(单个)对象的属性值。平均值将始终只是该属性的值。
Test-Connection
将接受一组名称作为-ComputerName参数,因此ForEach根本不是必需的。
$data = @("server1","server2","server3")
$results = Test-Connection $data -count 1 | select -ExpandProperty ResponseTime
答案 1 :(得分:0)
$FinalResult = @()
$data = @("server1","server2","server3")
foreach ($server in $data) {
$results = (Test-Connection $server -count 1 | Measure-Object -Property ResponseTime -Average).Average
$FinalResult += New-Object psobject -Property @{
ServerName = $server
Result = $result
}
}
$FinalResult | Select ServerName, Result | Export-Csv ".\FinalResult.csv" -NoTypeInformation
如果此片段不起作用,请告知我们,我会尽力而为。