0

I have been banging my head against a wall for a couple of days now trying to get Start-Job and background jobs working to no avail.

I have tried ScriptBlock and ScriptFile but neither seem to do what I want, or I can't seem to get the syntax right.

I have a number of recursive functions and need to split up the script to work in parallel accross many chunks of a larger data set.

No matter how I arrange the Start-Job call, nothing seems to work, and the recursive functions seem to be making everything twice as hard.

Can anyone give me a working example of Start-Job calling a recursive function and having multiple parameters, or point me somewhere where one exists?

Any help appreciated

1
  • 1
    It's not clear what is not working because you haven't shown us any code, or given us any output to go on. I'm not even sure I understand exactly what you are trying to do (Are you recursively starting jobs or just trying to start a script that calls a recursive function) or why multiple parameters are causing difficulty (what has multiple parameters? the recursive function or the script). A simple, complete example of what hasn't worked along with the output/error messages would resolve this. Commented Sep 10, 2014 at 3:37

1 Answer 1

2

This works for me:

$sb = {param($path, $currentDepth, $maxDepth) function EnumFiles($dir,$currentDepth,$maxDepth) { if ($currentDepth -gt $maxDepth) { return }; Get-ChildItem $dir -File; Get-ChildItem $dir -Dir | Foreach {EnumFiles $_.FullName ($currentDepth+1) $maxDepth}}; EnumFiles $path $currentDepth $maxDepth }
$job = Start-Job -ScriptBlock $sb -ArgumentList $pwd,0,2
Wait-Job $job | Out-Null
Receive-Job $job

Keep in mind your functions have to be defined in the scriptblock because the script runs in a completely separate PowerShell process. Same goes for any externally defined variables - they have to be passed into Start-Job via the -ArgumentList parameter. The values will be serialized, passed to the PowerShell process executing the job, where they will then be provided to the scriptblock.

Sign up to request clarification or add additional context in comments.

4 Comments

Yea but this doesn't have multiple parameters. Not sure why that is emphasized, but right now my guess would be that there is a call like EnumFiles($dir, $filter) which doesn't do what most people think it does.
If that's the case, Set-StrictMode -Version Latest should set the OP straight. :-)
I updated the answer to make the scriptblock take multiple parameters.
This is a recursive function that runs as one job in the background. I interpret the question in a way that there is a performance problem. SO what I think was asked is a recursive function that on each recursive call, splits itself into a job

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.