How to Schedule NAV Services to Restart
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
<
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.
Leave a comment