Jobs / Parallel Processing / Async

Hello all,

I am new to PowerShell jobs but I have a problem that I am trying to solve - and if I understand them correctly, I think it is a proper use. Ultimately, I have to upgrade firmware on 300+ routers. I have a script that will do this now in series, which will take a long time. I am thinking that if done in parallel it will be more efficient.

I have a script that will start a job, uniquely named for each router, SSH to the router, run a command, and schedule a reboot. I am using the Posh-Junos module to work with my routers (GitHub - scottdware/Posh-Junos: Powershell module to interact with Junos devices.).

My question is - when I run my script that creates the jobs, the output on the screen shows that all of the jobs are being created (45 in my script), but my powershell prompt does not return for almost 2 minutes after the last job is created. Why is this?

Here is the script I am using.

$routers = Import-Excel -path '.\routers.xlsx' -WorkSheetname routers | 
    Where-Object { $_.active -eq $true } | Select-Object -Property Shop, Router

Import-Module Posh-Junos 

$count = 15

foreach($r in $routers | select -First ($count * 3))
{
    $shop = $r.shop
    $jobname = "RunCommands-$shop"
    $block = {
        param($srx)

        $user = '***'
        $pass = '***'
        
        Write-Host "Connecting to:" $srx
        Invoke-JunosCommand -Device $srx `
                                    -User $user `
                                    -Password $pass `
                                    -Command 'request system software add /var/tmp/fw.tgz;request system reboot at 201701050100'
    }

    Start-Job -Name $jobname -ScriptBlock $block -ArgumentList $r.Router

}


Any input is appreciated.

Thanks,
sb

“My question is – when I run my script that creates the jobs, the output on the screen shows that all of the jobs are being created (45 in my script), but my powershell prompt does not return for almost 2 minutes after the last job is created. Why is this?”

There’s a bunch of overhead in spinning up those processes and creating pointers and buffers for them.

You might also look at launching your script from a Workflow. Those can parallel-ize as well, and it’s not PowerShell per se once it’s running, so it could perform a bit better.

Thanks Don!

I know it has been a few weeks - but I ended up finding a pretty good series on workflows on the Hey, Scipting Guy! block - here is the entire series (PowerShell Workflow–the complete series | Richard Siddaway's Blog)

This got me headed in the right direction. I now have something that seems to do 5 upgrades in parallel. Not really sure why it is stopping at 5 - but it is certainly better than it was.

Thanks again
-sb

Have to tried to compare the result if you use PoshRSJob?