Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to add results to an array across multiple functions in Powershell?
    primarykey
    data
    text
    <p>Follow-on question from <a href="https://stackoverflow.com/questions/20520980/powershell-array-scope-why-is-my-array-empty">here</a>. I want to perform a series of checks on some infrastructure, and if the check fails, add it to a list. At the end of the worflow, write the results list. Pseudo code:</p> <pre><code>Function CheckSomething { # perform a check here, if check failed add to the results list } Function CheckSomethingElse { # perform another check, if check failed add to the results list } Function ShowResults { $results; } CheckSomething; CheckSomethingElse; ShowResults; </code></pre> <p>I would like to avoid using global variables, how would you solve it? Use a <code>collections.arraylist</code>?</p> <p><strong>Update</strong> I tried the following suggestion from @mjolinor</p> <pre><code>Function CheckSomething { # perform a check here, if check failed add to the results list $check = $true if ($check -eq $true) {$results[CheckSomething] = 'Pass'} else {$results[CheckSomething] = 'Fail}' } Function CheckSomethingElse { # perform another check, if check failed add to the results list $check = $false if ($check -eq $true) {$results[CheckSomethingElse] = 'Pass'} else {$results[CheckSomethingElse] = 'Fail}' } Function ShowResults { $results; } $results = @{} CheckSomething CheckSomethingElse ShowResults </code></pre> <p>and I get:</p> <pre><code>Missing or invalid array index expression. At C:\Users\moomin\Documents\errorsTest.ps1:5 char:36 + if ($check -eq $true) {$results[ &lt;&lt;&lt;&lt; CheckSomething] = 'Pass'} + CategoryInfo : ParserError: ([:String) [], ParseException + FullyQualifiedErrorId : MissingArrayIndexExpression </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload