none
cannot return multiple value in powershell workflow RRS feed

  • Question

  • Given the following Script, how can i return two variables to the main code?

    I have tried "return" command but show an error. Please find the code and error massage below and also please advice.

    Code:

    workflow Test-MultiReturnVals
    {
    parallel
    {
    $a=@(Test-Connection 8.8.8.8 -count 3)  
    $b=@(Test-Connection 4.2.2.1 -count 3)
    return $a,$b
    }

    }
    $c,$d = Test-MultiReturnVals
    $c

    Error:

    Microsoft.PowerShell.Utility\Write-Error : The workflow was terminated by a Terminate activity.
    At RunScripts:9 char:9
    +
        + CategoryInfo          : NotSpecified: (:) [Write-Error], WorkflowReturnException
        + FullyQualifiedErrorId : Microsoft.PowerShell.Workflow.WorkflowReturnException,Microsoft.PowerShell.Commands
       .WriteErrorCommand
        + PSComputerName        : [localhost]



    • Edited by sagar.kubd Tuesday, November 21, 2017 5:22 PM
    • Moved by Bill_Stewart Thursday, January 25, 2018 10:41 PM Unanswerable drive-by question
    Tuesday, November 21, 2017 4:57 PM

All replies

  • You workflow is not valid for what you ask and is  not being used as intended.

    workflow Test-MultiReturnVals {
        parallel{
            Test-Connection 8.8.8.8 -count 3
            Test-Connection 4.2.2.1 -count 3
        }
    }
    Test-MultiReturnVals
    $results = Test-MultiReturnVals


    \_(ツ)_/


    • Edited by jrv Tuesday, November 21, 2017 5:31 PM
    Tuesday, November 21, 2017 5:31 PM
  • If for some reason you want to return separate collections then this will work but it defeats the purpose of a workflow.

    workflow Test-MultiReturnVals {
        parallel{
            $a = @(Test-Connection 8.8.8.8 -count 3)
            $b = @(Test-Connection 4.2.2.1 -count 3)
            ,$a
            ,$b
        }
    }
    $a,$b = Test-MultiReturnVals


    \_(ツ)_/


    • Edited by jrv Tuesday, November 21, 2017 5:39 PM
    Tuesday, November 21, 2017 5:37 PM
  • Thank you so much,

    its working, though get an error at the beginning therefore it is helpful

    error massage:

    At line:1 char:1
    +
    A workflow cannot use recursion.
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : RecursiveWorkflowNotSupported

    Tuesday, November 21, 2017 6:58 PM
  • There is no recursion in my example.  You must have changed something.

    \_(ツ)_/

    Tuesday, November 21, 2017 7:23 PM