Jeremy F.
Jeremy F.

Reputation: 1868

Powershell - pass function results to other parts of the script

New to Powershell.

I have a Powershell script that has a DO UNTIL loop. Inside that loop I call 2 functions. Based on the results of the functions determine if I can continue the script. My issue seems to be passing the result of the function back into the DO UNTIL loop for the loop to continue.

Here is the general idea:

Function MyFunction1
{
(Stuff happens here)
    IF($Good -eq "Yes"){
            Return 1
    }Else{
            Return 0
    }
}


Function MyFunction2
{
(Other stuff happens here)
    IF($ReallyGood -eq "Yes"){
            Return 1
    }Else{
            Return 0
    }
}

DO{
    $a = MyFunction1
    IF($a -eq 1){
      $b = MyFunction2
            IF($b -eq 1){
                        $endscript = "Yes"
                        }ELSE{
                        $endscript = "No"
                        }
    }Else{
    $endscript = "No"
    }   
}UNTIL(
$endscript -eq "Yes")

Upvotes: 0

Views: 708

Answers (1)

Jeremy F.
Jeremy F.

Reputation: 1868

Using this question: How to return several items from a Powershell function

I went with creating a variable within the function and setting the property to a number. Then assigning that variable a number. Finally, in the IF statement, I can reference that variable property.

Function MyFunction1
{
(Stuff happens here)

    $fun1_value = "" | Select-Object -Property number

    IF($Good -eq "Yes"){
            $fun1_value = 1
            Return $fun1_value
    }Else{
            $fun1_value = 0
            Return $fun1_value
    }
}


Function MyFunction2
{
(Other stuff happens here)

    $fun2_value = "" | Select-Object -Property number

    IF($ReallyGood -eq "Yes"){
            $fun2_value = 1
            Return $fun2_value
    }Else{
            $fun2_value = 0
            Return $fun2_value
    }
}

DO{
    $fun1 = MyFunction1
    IF($fun1.number -eq 1){
      $fun2 = MyFunction2
            IF($fun2.number -eq 1){
                        $endscript = "Yes"
                        }ELSE{
                        $endscript = "No"
                        }
    }Else{
    $endscript = "No"
    }   
}UNTIL(
$endscript -eq "Yes") 

Upvotes: 1

Related Questions