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.
MIT License
541 stars 87 forks source link

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

Open adamfortuno opened 6 years ago

adamfortuno commented 6 years ago

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?

MVKozlov commented 6 years ago

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