Jobs / Parallel Processing / Async

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Paal Braathen Paal Braathen 5 months, 3 weeks ago.

  • Author
    Posts
  • #61222
    Profile photo of stephenmbell
    stephenmbell
    Participant

    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 (https://github.com/scottdware/Posh-Junos).

    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

  • #61224
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #63015
    Profile photo of stephenmbell
    stephenmbell
    Participant

    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 (https://richardspowershellblog.wordpress.com/2013/02/13/powershell-workflowthe-complete-series/)

    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

  • #63055
    Profile photo of Paal Braathen
    Paal Braathen
    Participant

You must be logged in to reply to this topic.