Running Microsoft Dynamics NAV tasks in parallel with Powershell

As you know, in many scenarios I like to schedule NAV tasks by using Powershell instead of using the standard NAV Job Queue.

In many NAV implementations I have also some cases where I need to schedule different NAV long-running tasks that are totally independent from each others (for example, a tasks that updates a BI system, another that performs calculations in NAV and so on).

The Powershell NAV Job Scheduler I’ve showed in my previous post executes the tasks in a serial way (codeunit 50000, then codeunit 50001 and so on). If I’m sure that these codeunit performs tasks that are totally independent, why not try to schedule them with a parallel execution?

This can be achieved by using Powershell Workflows. We can create a Powershell Workflow like the following:

workflow NAVJobSchedulingWF {      
  parallel {        
    Invoke-NAVCodeunit navostp -CodeunitId 50000 -CompanyName 'EID' -ErrorAction Stop     
    Invoke-NAVCodeunit navostp -CodeunitId 50001 -CompanyName 'EID' -ErrorAction Stop        
    Invoke-NAVCodeunit navostp -CodeunitId 50002 -CompanyName 'EID' -ErrorAction Stop             
    }   
}

and then execute the workflow itself. The 3 NAV codeunits will be executed in a parallel way (this speeds up some data processes).

A complete Powershell script is as follows:

Import-Module 'C:\Program Files\Microsoft Dynamics NAV\100\Service\Microsoft.Dynamics.Nav.Management.dll'Import-Module 'C:\Program Files\Microsoft Dynamics NAV\100\Service\Microsoft.Dynamics.Nav.Management.dll'
$Logfile = "C:\TEMP\$(gc env:computername).log"
Function WriteLogFile
{
  Param ([string]$logstring)
  $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")$logstring = $Stamp + ': ' + $logstring
  Add-content $Logfile -value $logstring
}
workflow NAVJobSchedulingWF 
{      
  parallel 
  {        
    Invoke-NAVCodeunit navostp -CodeunitId 50000 -CompanyName 'EID' -ErrorAction Stop      
    Invoke-NAVCodeunit navostp -CodeunitId 50001 -CompanyName 'EID' -ErrorAction Stop        
    Invoke-NAVCodeunit navostp -CodeunitId 50002 -CompanyName 'EID' -ErrorAction Stop             
  }   
}
 
try
{
  NAVJobSchedulingWF 
}
catch [Exception]
{  
  WriteLogFile "Error returned from NAV: $PSItem"
}

PowershellWF_01

By using a Powershell workflow you can create very complex task processes, maybe also where some tasks are executed in a parallel way and others are executed sequentially.

For example:

PowershellWF_02

Here, for every server listed as parameter in this workflow, the two Powershell commands are executed sequentially (sequence clause). The process for every server is executed in a parallel way.

Nice isn’t it?

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s