Restart Remote NAV Services

  1 minute read

When I deploy changes to Production or UAT, one step I usually do is to restart the NAV services, that way changes are synchronized properly.

But usually, the clients have more than one server so you need to make sure we login into all those servers and restart NAV services installed on them, so I was looking for a script that will restart all the NAV services without logging into all those servers.

I found the below blog from Marcell which does something similar but he used the script to get the service information, I just modified his original script for my needs but all the credit goes to him.

Here is his blog link

Below are the two scripts which are used to restart NAV services, first is the function which we need to execute before executing the second script.

In the second script, you specify server names and NAV version, the version is used to import the modules.

function Restart-NavServicesRemotely
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [int] $NavVersion,
        [Parameter(Mandatory=$true)]
        [array] $Severs
    )

    Process
    {
        $Arguments = ($NavVersion) 
        $ServiceConfigurations

        foreach($Server in $Servers)
        {
            Write-Host "Connecting To $($server)..." -ForegroundColor Cyan
        
            $LineNo = 1

            $RemoteSession = New-PSSession -ComputerName $Server -EnableNetworkAccess

            Invoke-Command -Session $RemoteSession -ArgumentList $Arguments `
            -ScriptBlock{
                Import-Module "c:\Program Files\Microsoft Dynamics NAV\$($args[0])\Service\NavAdminTool.ps1" | Out-Null
                $ServiceConfigurations = New-Object System.Collections.ArrayList

                Write-Host '..............................' -ForegroundColor Cyan
                Write-Host 'Restarting services...' -ForegroundColor Cyan
                Write-Host '..............................' -ForegroundColor Cyan



                $Services = Get-NAVServerInstance 
                foreach ($Service in $Services)
                {
                    $LineNo++
                    
                    Restart-NAVServerInstance -ServerInstance $Service.ServerInstance                  

                }  
                return $ServiceConfigurations
            }
        }    
    }
}

Second Script:



$Servers = ('server1','server2','server3')
Restart-NavServicesRemotely -NavVersion 110 -Severs $Servers | Format-Table -AutoSize

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

Leave a comment