Melon Man
Melon Man

Reputation: 185

Powershell - Store Multiple Results from Foreach loop into an Array

I currently have a Foreach loop that is running two different tests. If the first test is successful, then it attempts the second one and returns a result. My question is, how can I store the results from both tests within the Foreach loop into a variable, and output those into an array? Here's an example below of the code that I'm currently testing.

foreach ($e in $example)
{
 if((Example test ) -eq $true){
        $test2 = new-object 'test' $e
        if($test2.result -eq 'example2')
        {
        return $true
        }
        else
        {
            Write-host $e 'N/A'
            return $false

        }
        $test2.stop()
         }
else{
write-host 'N/A2'
return $false
}
}

I was wondering how to store the test results either $true or $false into variables to store into an array for each test in $e. Would storing them into an array like below work? Any input would be helpful, thanks!

$result = @(foreach ($e in $example){
})

Upvotes: 0

Views: 2941

Answers (1)

js2010
js2010

Reputation: 27418

I suppose you can do:

$result = foreach($i in 1..10) { 
  [pscustomobject]@{result1 = $true
                    result2 = $true
  }
}
$result


result1 result2
------- -------
   True    True
   True    True
   True    True
   True    True
   True    True
   True    True
   True    True
   True    True
   True    True
   True    True

Upvotes: 1

Related Questions