Wednesday, October 18, 2017

How to Schedule NAV Services to Restart

clock-2174116_1920

Recently I was asked if there is a way to schedule a NAV service to restart at a specific time, and for that I have used a small PowerShell script which I have scheduled using windows task scheduler.

Below is the script and steps for that

This example is based on NAV 2013 but the same code can be used for other versions

The below  PowerShell script is used to restart NAV Services, since in our case there are more then one NAV instance on the server, I have used a for loop to find all the available/running instances and then filter those instances using like statement, in this case I am only restarting NAV Instances with the word “Test” in their name. You can replace this with any other word or remove that condition.

Import-Module 'C:\Program Files\Microsoft Dynamics NAV\70\Service\NavAdminTool.ps1' -WarningAction SilentlyContinue | Out-Null
$Instances = Get-NAVServerInstance
for ($i = 0; $i -le $Instances.Count - 1; $i++) {    
     if ($Instances[$i].State -eq 'Running' -And $Instances[$i].Name -like '*Test*') {       
         Restart-Service $Instances[$i].Name
         #Write-Output $Instances[$i].Name
     }
}

Save the above script in a file and store in a location then we need to create a Task Scheduler to execute the above script.

Create a new task using the option shown in Fig 1, then choose the name, and Trigger when you want to run, under the Actions tab choose Start a Program  (Fig 2) and for the program use Powerhsell.exe and specify the ExecutionPolicy ByPass and path for the script in arguments

image

Fig 1


image

Fig 2

image

Fig 3


This will create a task in windows scheduler to restart the services, you can also export the task as .xml and import into another server.

If you have any other tips or suggestions, please do share them in the comments below.

Share:

3 comments:

Jimy said...

Great tip basing on windows task sheduler. Very nice post with a lot of educative content. IT students should definitely have a look at this,

Anonymous said...


Hi, great article. Helped me a lot.

Thanks

Anonymous said...

in your script it says C:\Program Files\Microsoft Dynamics NAV\70\Service\NavAdminTool.ps1. does this mean i need to install admin function of nav as well? because i dont have service folder and navadmintool.ps1 on my nav 2018