How to use Powershell together with NAV Service Tiers

Today my boss was told we had to restart some NAV services every week, check whether it reconnects and advise people that the services is up again. I was asked to find a solution how to schedule restart of some NAV service tiers and letting people know whether they reconnected as expected.
After slamming my head against the wall shouting out loud something about fixing the memory leak in the code instead. I had to say it was stupid, but a interesting assignment and came up with the following the idea to use a Powershell script to restart the service tiers on different servers and collect messages from the event viewer after the restart session is completed.

Prerequisites:

  • Web Server
  • Windows Scheduler
  • Powershell editor
  • Administrative rights

I created to Powershell script the first one will be scheduled to run once a week, it will restart the NAV Services and second script will pull the needed event from the Event Viewer, creating a HTML-file and copy it to a web server.
When the scripts was completed I created two scheduled task to start the scripts under Actions I choice Powershell.exe with the argument -Command C:\scripts\NAVRestartService.ps1″.

Service Restart Script:

$Servers = 'Serv01', 'Serv02'

$Servers |
ForEach-Object{
Get-Service ‘MicrosoftDynamicsNavServer$NAS*’ -ComputerName $_| Restart-Service
}

Webpage generator script:

$Header = @"

“@

$TimeCreated = Get-Date

$Servers = ‘Serv01’, ‘Serv02′

$Servers |
ForEach-Object{
Get-WinEvent -FilterHashtable @{LogName=’Application’;Id=206,208,209,210,214,215,218,219,221,225} -MaxEvents 20 -ComputerName $_
} |
Select-Object TimeCreated, ID, MachineName, Message |
ConvertTo-Html -Head $Header -Body “Page created: $TimeCreated”|
Out-File C:\inetpub\wwwroot\navservices\index.htm

 

I you enjoyed reading.

Brian Lind