Restarting Dynamics NAV Service Tiers with PowerShell

A quick tip about managing Microsoft Dynamics NAV server instances. Yes, it involves PowerShell 🙂

I’ve been working on a project where we’ve needed a lot of server instances recently. We’ve got three databases. Each database has an instance for Windows authentication, one for NavUserPassword authentication, another to run a job queue, another to serve up SOAP services using NTLM…we’ve got a lot of services.

Occasionally they need restarting – for licence changes, add in changes etc. You could use the administration tool to restart them one at a time – but you’re not a chump – you’ll use PowerShell instead.

Having loaded the Nav.Management.dll into PowerShell ISE you can do the following:

Get-NAVServerInstance | Where-Object –Propert ‘State’ –eq ‘Running’ | Set-NAVServerInstance -Restart

This one liner will find server instances running on the local server, filter them to return only those that are running and send each of them a restart command.

That’s fine if you just want to restart them. How about if you need to leave them stopped, to swap some add-in files for instance, and then start them again? You only want to start the services that were originally running.

Try this for size in the script pane:

$ServerInstances = Get-NAVServerInstance | Where-Object -Property 'State' -eq 'Running'

$ServerInstances | Set-NAVServerInstance -Stop

#uncomment this line to start the previously stopped services
#$ServerInstances | Set-NAVServerInstance –Start

The first time you execute this script the results of the first line i.e. the server instances that are running, will be saved into the $ServerInstances variable.

You can use that variable to tell the Set-NAVServerInstance command which server instances to stop. This variable retains its value in the editor after the script has finished executing.

When you’ve finished doing whatever it is you’re doing and are ready to fire the instances back up, comment the second line (with a #) and uncomment the last line. The same server instances that were previously stopped will be started again.

Leave a Reply