5
$data = @("server1","server2","server3")

foreach ($server in $data) {
    $results = (Test-Connection $server -count 1 | Measure-Object -Property ResponseTime -Average).Average
}

I'm trying to figure out how to take the output of a ForEach and store the results into an array so I may then compare them later. I could not come up with a way to do this yet.

8
  • 7
    $results = @( ForEach ( ... ) { ... } ) Commented Feb 7, 2017 at 17:39
  • @PetSerAl, whats the difference between $results = @( foreach ...) and $results = foreach ..., doesn't it return array anyway? also, shouldn't foreach return something, for this to work? Commented Feb 7, 2017 at 17:57
  • @4c74356b41 @() always return array. Even in case of zero or one item. Commented Feb 7, 2017 at 17:59
  • so how does a zero length array help compare results? Commented Feb 7, 2017 at 18:02
  • @4c74356b41 It helps by eliminating special case, when with zero items PowerShell does not return array by default. Commented Feb 7, 2017 at 18:17

2 Answers 2

3
    $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

Please let me know if this snippet doesn't work, i will do the needful.

Sign up to request clarification or add additional context in comments.

1 Comment

This is extremely inefficient because appending creates a new array every single loop, resulting in a time complexity of O(n^2).
1

By doing the ForEach, you're just measuring the value of the property of a scalar (single) object. The average will always be just the value of that property.

Test-Connection will accept an array of names as the -ComputerName parameter, so the ForEach isn't really necessary at all.

 $data = @("server1","server2","server3")

 $results = Test-Connection $data -count 1 | select -ExpandProperty ResponseTime

2 Comments

I used the example to get the concept of what I'm doing. I'm not using Test-Connection, but a more larger script, but the concept is the same. Just need to figure out how to store results into an array while having it loop until there's no more inputs.
Then you already have your answer in the comments. Simply redirect the foreach output to a variable, and Powershell will automatically build an array if it returns more than one object. You can force it to be an array using the @() syntax if that's necessary to prevent errors downstream in the case of it not returning multiple objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.