1

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 workflow, write the results list. Pseudo 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;

I would like to avoid using global variables. How would you solve it? Use a collections.arraylist?

Update

I tried the following suggestion from @mjolinor

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

And I get:

Missing or invalid array index expression.
At C:\Users\moomin\Documents\errorsTest.ps1:5 char:36
+    if ($check -eq $true) {$results[ <<<< CheckSomething] = 'Pass'}
    + CategoryInfo          : ParserError: ([:String) [], ParseException
    + FullyQualifiedErrorId : MissingArrayIndexExpression

This is a follow-on question from here.

1
  • What type of variable you use has no bearing on the use of a global/non-global variable. Some people would say that there is nothing wrong with a global variable or singleton object. A lot of times they would be right. Personally I tend to avoid them because I feel it makes my code more portable. The way to avoid one in your case is to return the failed list as an array and add it to an array that is technically in the global scope but not being directly accessed in the individual functions. Commented Dec 11, 2013 at 16:05

2 Answers 2

1

Another option is to use a hash table:

Function CheckSomething
{
   # Perform a check here. If check failed, add to the results list.
   if ($check -eq $true) {$results['CheckSomething'] = 'Pass'}
   else {$results['CheckSomething'] = 'Fail}'
}

Function CheckSomethingElse
{
    # Perform another check. If check failed, add to the results list.
    if ($check -eq $true) {$results['CheckSomethingElse'] = 'Pass'}
    else {$results['CheckSomethingElse'] = 'Fail}'
}

Function ShowResults
{
    $results;
}

$Results = @{}
CheckSomething
CheckSomethingElse
ShowResults
Sign up to request clarification or add additional context in comments.

8 Comments

This is still using a global variable which the OP wanted to avoid.
It is using a variable in a parent scope, but that is not the same thing as using an explicitly global variable.
That's my fault. The name of the hash table key needs to be quoted. I've updated the posted script.
@mjolinor you are correct that it is not declared as a global. It still couples the functions just as much as a global though.
In this application, where there is a separate function to display the results some kind of coupling seems unavoidable.
|
0

Referencing your initial post, I believe the simplest way would be to take your original approach and use the $Script: scope:

Function CheckSomething
{
    # perform a check here, if check failed add to the results list
    $Script:results += 'Added Content'
}

Function CheckSomethingElse
{
    # perform another check,  if check failed add to the results list
    $Script:results += 'Added Content'
}

Function ShowResults
{
    $results;
}

$results = @();
CheckSomething;
CheckSomethingElse;
ShowResults;

Since you are defining $results at the script level already, you just need to make sure that inside your functions you are referencing that variable at the appropriate scope by append $Script:.

Comments

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.