PoshRSJob icon indicating copy to clipboard operation
PoshRSJob copied to clipboard

Need help passing variables between jobs

Open hou8182 opened this issue 6 years ago • 3 comments

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"

​     }


}

hou8182 avatar Sep 03 '18 11:09 hou8182

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

p.s. globals is bad :)

MVKozlov avatar Sep 03 '18 13:09 MVKozlov

@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.

ALuckyGuy avatar Oct 11 '18 15:10 ALuckyGuy

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

MVKozlov avatar Oct 11 '18 18:10 MVKozlov