proxb / PoshRSJob

Provides an alternative to PSjobs with greater performance and less overhead to run commands in the background, freeing up the console and allowing throttling on the jobs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Start-RSJob Passing a Script w/Script Scope Variables in FunctionFilesToImport

adamfortuno opened this issue · comments

commented

On a machine in a distant land, exists a script named thing.ps1 with content:

$script:foo = 'abc'

function f { return $script:foo }

If I attempt to run f from my interactive session, I see "abc" like I'd expect:

image

If I try to run it via Start-RSJob, I see nothing...

Start-RSJob { f } -Name 1 -FunctionFilesToImport $(ls .\thing.ps1).fullname
Receive-RSJob -name 1

image

The value $script:foo in the script passed to Start-RSJob returns as $null.

Can you not use variables in the script scope?

for function f variable $script:foo - like a global variable.
RSJob can not import any variables that used inside functions if it not local.

Btw, it is a bad practice.

as a workaround you can use truly global variables, function parameters or modules (variable defined in a module will be imported with module