Start-RSJob Passing a Script w/Script Scope Variables in FunctionFilesToImport
adamfortuno opened this issue · 1 comments
adamfortuno 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:
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
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
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