Coffee Break: Proactive Monitoring with Task Scheduler

Here in Part V of  the “Monitoring and Admin” coffee breaks, we will schedule a Windows PowerShell script with these two purposes:

  • Check for heartbeat (check if Dynamics NAV is still running), and
  • Check for a threshold value, for example MEM or CPU exceeding a max value

Customer Story:

We're a system administrator who is monitoring a large number of Dynamics NAV services. Rather than spending every morning checking that everything is OK, we can schedule checks. Then we only need to take action if something is not working OK. This can also enable a more proactive support, where the system administrator is notified of problems before users notice anything.


As a simple form for heartbeat: Just invoke Codeunit 1, Function Run, as this function will exist in every Dynamics NAV installation. In good times it will just run. If it doesn’t then we know that something is wrong, and that there is a need to check. Run it like this:

Import-Module 'C:\Program Files\Microsoft Dynamics NAV\90\Service\Microsoft.Dynamics.Nav.Management.dll'
Invoke-NAVCodeunit dynamicsnav90 -CodeunitId 1

Also see further details here (whether you are using SCOM or not):

Now this script will just error on the screen if it fails. So here is a version that will send a notification by email instead:

Import-Module 'C:\Program Files\Microsoft Dynamics NAV\90\Service\Microsoft.Dynamics.Nav.Management.dll'
$EmailList = '',''
$SMTPServer = ''
Invoke-NAVCodeunit dynamicsnav90 -CodeunitId 1 -ErrorAction Stop
Send-MailMessage -To $EmailList -Body "NAV Server is not running." -Subject "NST WARNING" -from '' -SmtpServer $SMTPServer

Schedule it

An easy way to schedule a PowerShell script is, to use Windows Task Scheduler. Open Task Scheduler, then on the right hand side click Create Task. Give it any name, then on the Triggers tab, click New, and set a trigger to run daily and repeat for every 5 minutes. Under Actions, leave it to Run a program, type in PowerShell, then add an argument that is a PowerShell script like the one above, that you saved (.ps1).

Check for Threshold

Let’s say a Dynamics NAV Service frequently increases CPU or MEM usage and at this time the System Administrator needs to take some action. If CPU and MEM is OK then just log it to create a baseline. Here is an example of a script to do this:

$EmailList = '',''
$SMTPServer = ''
$LogFile = 'C:\Temp\NAVLog.txt'

$Result = Get-Process | Where-Object {$_.Name -like "*NAV*" -and $_.Path -like "*90*"}
if ($LogFile) {Get-Date | out-file -FilePath $LogFile -Append ; $Result | out-file -FilePath $LogFile -Append }
if ($Result.WorkingSet -gt 700000000)
Send-MailMessage -To $EmailList -Body "Memory is too high." -Subject "Memory high" -from '' -SmtpServer $SMTPServer
if ($Result.CPU -gt 5)
Send-MailMessage -To $EmailList -Body "CPU is too high" -Subject "CPU high" -from '' -SmtpServer $SMTPServer

As shown above, you can set this to run every 5 minutes via Windows Task Scheduler, then let that do some of the routine work for you.


Best regards

Jasminka Thunes, Escalation Engineer Dynamics NAV EMEA

Lars Lohndorf-Larsen, Escalation Engineer Dynamics NAV EMEA

Bas Graaf, Senior Software Engineer Dynamics NAV


PS: Going forward, we'll post these coffee breaks as:


Comments (9)

  1. Hi,
    will that also work with Dynamics NAV 2013 ?

    1. navteam says:

      No, this applies to Dynamics NAV 2016 only.

      1. Cláudio Freitas says:

        Hi guys,

        Can i use the Dynamics NAV 2016 management powershell module to invoke a NavCodeUnit on a Dynamics Nav 2013 or 2009 server?

        Thanks in advance,
        Best regrads.

    2. guido robben says:

      This works in NAV 2015

  2. Hi.
    Very nice post indeed!
    One question: can this monitoring be done using SCOM (System Center Operations Manager)?


  3. Isak Sigurdsson says:

    Is there any way to do this without setting the service tier user as a NAV user ?


  4. Daniel Berg says:


    I want to share my modified script.
    At first I added a check, if it’s running with administrator- priviliges, Secon I made an array-list, where you can enter your names of NAV-instances you want to check.

    To get the Administrator Mode working you need two files with the same name. One of them is the powershell Script and the Second is a batch file, which opens the powershell with administration privileges.

    Check Instances.cmd
    @echo off
    echo This programm needs Administration rights!
    PowerShell.exe -NoProfile -Command “”

    Check Instances.ps1
    Import-Module ‘C:\Program Files\Microsoft Dynamics NAV\90\Service\Microsoft.Dynamics.Nav.Management.dll’
    if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”))
    Write-Output ‘The check from the NAV instances has started’

    $Instance_List= @(“instance1″,”instance2”)
    for($i=0;$i -lt @($Instance_List).length;$i++)
    invoke-NAVCodeunit $Instance_List[$i] -CodeunitId 1
    Write-Output ‘The programm needs administration rights!’

    Daniel Berg

    the source of the .cmd file:

  5. Jens Saustrup says:

    Thanks for the good script.
    I had it running in our setup for two weeks monitoring our NAV 2016 queue instances.
    I ran into an issue where the call to Codeunit 1 still succeeds but all queues were stopped for some reason hence the services were not restarted.
    I have decided to go for monitoring heartbeat instead.

Skip to main content