Restart Dynamics NAV Services with Powershell

In this blog post I explain how you can restart Dynamics NAV services with Powershell, while also exploring a few of the available Cmdlets for selecting services and properties.

Restarting a NAV service

Restart-Service is a standard Powershell Cmdlet that will stop and then start Windows services. If the service is already stopped, then it will simply start the service.

Restarting the default NAV 2017 service can be done as follows:

PS C:\Windows\System32>Restart-Service 'MicrosoftDynamicsNavServer$DynamicsNAV100'

The unnamed parameter ‘MicrosoftDynamicsNavServer$DynamicsNAV100’ is the service name, this is always the Dynamics NAV Server instance name prefixed with MicrosoftDynamicsNavServer$.

NAV Service Properties
The properties of the default NAV 2017 service

Restarting multiple NAV services

Restarting multiple services can be done simply by using the * wildcard in the service name parameter:

PS C:\Windows\System32>Restart-Service 'MicrosoftDynamicsNavServer*'

The above command will restart every service with a name beginning with MicrosoftDynamicsNavServer.

So far the services selected for restart have been very indiscriminate, we’ve simply restarted all NAV services on the machine… What if we need to be a bit more selective?

Get-Service provides us with the ability to get objects representing Windows services. To get all services on your machine run Get-Service with no parameters:

PS C:\Windows\System32>Get-Service

The above command will print out a list of services and there statuses:

Output of the Get-Service Cmdlet

The same wildcard used for the Restart-Service Cmdlet can be used with Get-Service using the -Name parameter:

PS C:\Windows\system32> Get-Service -Name 'MicrosoftDynamicsNavServer*'

The benefit of using Get-Service however, is the ability to pipe the output to the Where-Object Cmdlet:

PS C:\Windows\system32> Get-Service -Name 'MicrosoftDynamicsNavServer*' | Where-Object {$_.Status -eq "Running"}

We now have a list of services starting with the name “MicrosoftDynamicsNavServer”, with a status of running:

Restart Dynamics NAV Service with Powershell
Get-Service running services

To keep things consistent, lets delegate all the filtering to Where-Object:

PS C:\Windows\system32> Get-Service | Where-Object {$_.Status -eq "Running" -and $_.Name -like 'MicrosoftDynamicsNavServer*'}

Get running NAV services with Get-Service and Where-Object

The output of Where-Object can be piped into Restart-Service.. so by piping Get-Service into Where-Object into Restart-Service, we can now restart all currently running NAV services with the following:

PS C:\Windows\system32> Get-Service | Where-Object {$_.Status -eq "Running" -and $_.Name -like 'MicrosoftDynamicsNavServer*'} | Restart-Service

Get-NAVServiceInstance

As always, there are many ways to skin a cat.. We could also approach this problem from another direction. The Get-NavServerInstance Cmdlet is available in the NavAdminTool.ps1 script file found in the NAV service folder. The script file will need to be loaded into your Powershell environement using the Import-Module Cmdlet:

PS C:\Windows\system32> Import-Module 'C:\Program Files\Microsoft Dynamics NAV\100\Service\NavAdminTool.ps1'

If you get the following error:

Execution policy error

You can change the execution policy using the Execution-Policy Cmdlet. In the following image you can see that I get the current execution policy for reference and then after changing the execution policy, I am able to run the Import-Module Cmdlet:

Powershell change execution policy

For security reasons it’s good practice to change the execution policy back:

PS C:\Windows\system32> Set-ExecutionPolicy -ExecutionPolicy Restricted

Get-NavServerInstance returns a list of NAV services, but the output is not compatable with the Restart-Service Cmdlet:

Output of Get-NavServerInstance

We can however pipe the output into the Select-Object Cmdlet which we can use to get the service names as string objects:
PS C:\Windows\system32> Get-NavServerInstance | Select-Object -ExpandProperty "ServerInstance"

Get-NavServerInstance ServerInstance Property

The string objects can then be piped directly into the Restart-Service Cmdlet:

PS C:\Windows\system32> Get-NavServerInstance | Select-Object -Expand "ServerInstance" | Restart-Service

Or we can pipe into Get-Service first, followed by Where-Object for more filtering options:

PS C:\Windows\system32> Get-NavServerInstance | Select-Object -ExpandProperty "ServerInstance" | Get-Service | Where-Object {$_.Status -eq 'running'} | Restart-Service

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.