powershellpowershell-workflow

Call a function from an InlineScript


How can I call a function in a workflow from a nested InlineScript? The following throws an exception because the function is out of scope in the InlineScript:

Workflow test
{
    function func1
    {
        Write-Verbose "test verbose" -verbose
    }

    InlineScript
    {
        func1
    }
}
test

Solution

  • "The inlinescript activity runs commands in a standard, non-workflow Windows PowerShell session and then returns the output to the workflow."

    Read more here.

    Each inlinescript is executed in a new PowerShell session, so it has no visibility of any functions defined in the parent workflow. You can pass a variable to workflow using the $Using: statement,

    workflow Test
    {
        $a = 1
    
        # Change the value in workflow scope usin $Using: , return the new value.
        $a = InlineScript {$a = $Using:a+1; $a}
        "New value of a = $a"
    }   
    
    Test
    
    PS> New value of a = 2
    

    but not a function, or module for that mater.