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

Need help passing variables between jobs #191

Open hou8182 opened 6 years ago

hou8182 commented 6 years ago

I'm creating a test using a nested multithreaded job. I'm having trouble assuring that the value that I have in $message2 being made available in the nested job. I don't have a problem with the script creating the text file in the directory that I specified, its just that the value turns up blank when I open the text file. By the way, I've also tried adding $using:message2 , but this just threw an error:

Start-RSJob : The value of the using variable '$using:message2' cannot be retrieved because it has not been set in the local session.


$ParentFolder = "C:\users\Family\Desktop\PII Searcher"

$Parentfolder | Start-RSJob -Name {"$($_)"} -Throttle 15 -ScriptBlock{
Param($targetfolder)

$message = "hello"
$global:message2 = "hello2"
$message | Out-File "$targetfolder\message.txt"

     $targetfolder | Start-RSJob -Name {"$($_)"} -Throttle 15 -ScriptBlock{
     Param($targetfolder)

     $message2 | Out-File "$targetfolder\message2.txt"

​     }

}
MVKozlov commented 6 years ago

use -VariablesToImport message,message2 on both Start-RSJob calls btw, why you need nested jobs ?

p.s. globals is bad :)

ALuckyGuy commented 5 years ago

@hou8182 I don't think the $targetfolder parameter is working the way you think. Start-RSJob doesn't pass the pipelined objects into the job as a parameter Old versions used to but it changed quite some time ago. Look at this post that explains how parameters work and how you can pass parameters. You can pass message2 into your job but you'll probably want to use the -ArgumentsList parameter.

MVKozlov commented 5 years ago

If the scriptblock have only one parameter then rsjob insert pipelined object into this parameter