Calling a function from 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 of InlineScript:

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

    InlineScript
    {
        func1
    }
}
test
+4
source share
2 answers

"The inlinescript operation runs commands in a standard Windows PowerShell session without a workflow, and then returns the result to the workflow."

More details here .

Each inlinescript runs in a new PowerShell session, so it does not have the visibility of any functions defined in the parent workflow. You can pass the variable to the workflow using instructions $Using:,

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

.

+3

, powershell :

workflow Hey
{
   PrepareMachine
   ConfigureIIS
}

function PrepareMachine() {
  Import-Module "MyCommonStuff"
  CallSomethingBlahBlah()
}

function ConfigureIIS {
  Import-Module "MyCommonStuff"
  CallSomethingBlahBlah2()
}

, , :

workflow Hey 
{
   InlineScript {
     func1
  }
}

function func1 {
  Write-Output "Boom!"
}

, . , , . - , . rant , , :)

0

Source: https://habr.com/ru/post/1584401/


All Articles