How to use functions from a file in a background-job scriptblock How to use functions from a file in a background-job scriptblock powershell powershell

How to use functions from a file in a background-job scriptblock


The created job has its own scope that does not inherit the functions defined in your local scope. You can either load the functions in your job script block or use the -InitializationScript parameter.

# Option 1:$ip="10.0.0.24"$scriptblock = {get-ostype -ip $args[0]}$initializationscript = {. c:\path\functions.ps1}Start-Job -InitializationScript $initializationscript -ScriptBlock $scriptblock -ArgumentList $ip# Option 2:$ip="10.0.0.24"$scriptblock = {. c:\path\functions.ps1; get-ostype -ip $args[0]}Start-Job -ScriptBlock $scriptblock -ArgumentList $ip