Function not accessible in a ScriptBlock Function not accessible in a ScriptBlock powershell powershell

Function not accessible in a ScriptBlock


Start-Job run jobs in separate PowerShell processes. So that, jobs do not have access to session state of calling PowerShell session. You need to define functions, which get used by jobs, in every job. An easy way to do that without duplicating the code would be using of -InitializationScript parameter, where all common functions can be defined.

$IS = {    function CommonFunction1 {        'Do something'    }    function CommonFunction2 {        'Do something else'    }}$SB1 = {    CommonFunction1    CommonFunction2}$SB2 = {    CommonFunction2    CommonFunction1}$Job1 = Start-Job -InitializationScript $IS -ScriptBlock $SB1$Job2 = Start-Job -InitializationScript $IS -ScriptBlock $SB2Receive-Job $Job1,$Job2 -Wait -AutoRemoveJob


Just extending PetSerAl's answer, you can use Runspaces for this, if you want faster code and a little bit more organised. Check out this question:39180266

So when you run something in different runspace, you need to import functions in both of them. So finished structure would look like:

  1. Module: functions.ps1 - you store here functions to share with both scopes.
  2. Main script: script.ps1 - it's basically your script, with runspaces, but without functions from functions.ps1.

And in beginning of your script.ps1, just simply call Import-module .\functions.ps1, to get access to your functions. Remember that runscape has different scope, and in their scriptblock, you have to call import-module once again. Full example:

#file functions.ps1function add($inp) {     return $inp + 2}#file script.ps1Import-module .\functions.ps1        #or you can use "dot call": . .\function.ps1Import-module .\invoke-parallel.ps1  #it's extern module$argument = 10                       #it may be any object, even your custom class$results = $argument | Invoke-Parallel -ScriptBlock {    import-module .\functions.ps1    #you may have to use here absolute path, because in a new runspace PSScriptRoot may be different/undefined    return (add $_)                  # $_ is simply passed object from "parent" scope, in fact, the relationship between scopes is not child-parent}echo $result # it's 12echo (add 5) # it's 7