0

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){
})

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, I ended up doing it another way, but using a psobject, and adding a variable with the result for each test ended up working. Thanks for your help!
@MelonMan - what did your solution end up being? I have a feeling it would help me with my current issue.

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.