Named Childjobs?

Hi,

I have been using the Foreach-object -Parallel for quite some time now, and are generally happy with it.
But i would like to name the childjobs, with a name of my choosing.

The code will generate a set of childjobs called “Job1, Job2, Job3, etc”

$RunspaceJobId = @("Server1","Server2","Server3") | ForEach-Object -ThrottleLimit 10  -Parallel {
    $_
} -AsJob | Wait-Job
Get-Job -IncludeChildJob -Id $RunspaceJobId.Id

I would like to be able to name my childjobs, so that they are for instance named based on my input - how can i achieve that?

TBH I’m not sure if that’s possible, but maybe someone else has done it.

If you want to use -Parallel then it makes little sense to do them as jobs. That is the purpose of parallel to run them all at once. If you’d prefer jobs with custom names, just handle the jobs yourself and forego -Parallel

$joblist = @("Server1","Server2","Server3") | ForEach-Object {
    Start-Job -Name $_ -ScriptBlock {
        Param($Variable)
        # code that you would've been running in the foreach-object originally

        "$variable is what was passed into argumentlist"
    } -ArgumentList $_
}

$joblist | Get-Job

May I ask why? What would be your benefit?

And … depending on the amount of jobs you usally create I’d say it’s a little bit like the difference between cattle and pets. The ones you give names and take care of individually. While the others are a kind of faceless homogenous mass you actually don’t see as individual beings.

That’s an interesting, but accurate analogy. :slight_smile:

I borrowed it :blush: … from Jeffrey Snover.

Thanks for the helpful inputs :slight_smile:

So i am using the Jobs to pull some info from all our SQL servers, but want to limit it to 10-20 concurrent tasks (therefor the -Parallel & -ThrottleLimit 10 - might be incorrect, but it was the best example i could get to work.)

Sometimes the query for a given SQL server fail for whatever reason, and the solution is then to go through the childjobs that have a failed state, and see what happened.
It would be nice to have the job named as the SQL instance, so that i can just by looking at the name, see what server it have failed on - its not critical at all, but had hoped it would be possible :slight_smile:

You don’t need to name your jobs you need a proper error handling where you get the server name provided. :man_shrugging:t3: … wouldn’t you agree? :wink: :wink: :love_you_gesture:t3:

I luckily got a proper error handling in place, and its nicely logged to a database - so i agree on that part :wink:
But had still hoped, that i could name the child jobs :slight_smile:

Anyways - thanks for the help! Its greatly appreciated!

So you don’t provide the name of the SQL instance in your error handling? Really? I am confused now.

Have you tried Doug’s code suggestion?

I believe that only names the primary job. As far as I know you cannot name child jobs. Any job created always has at least one child job iirc.