Dynamics NAV Microsoft PowerShell SQL Server

Import-NAVEncryptionKey : Cannot establish a connection to the SQL Server/Database

If you are sure that the values inserted in the parameters are correct but you are receiving the following error message:

Import-NAVEncryptionKey : Cannot establish a connection to the SQL Server/Database.

This could be due to one of the following reasons:
* SQL Server is not started.
* The database does not exist.
* Proper permissions have not been given to the NAV Server Account.
Try again later or contact your system administrator.
At line:1 char:1
+ Import-NAVEncryptionKey
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (0:Int32) [Import-NAVEncryptionKey], NavCSideException
+ FullyQualifiedErrorId : Microsoft.Dynamics.Nav.Management.Cmdlets.ImportNavEncryptionKey

Maybe the user you are using to run the script does not have access to the db, this is a common issue when you use ‘Database Authentication Mode = SQL Server Authentication’. To be safe use the same account you set up in the instance for the ‘Database Credentials’.

Run Windows PowerShell ISE as Administrator and run the following script changing placeholders with the angle brackets (<>) with your values

Set-ExecutionPolicy unrestricted -Force
Import-Module 'C:\Program Files\Microsoft Dynamics NAV\<NavVersion>\Service\NavAdminTool.ps1'

$Credential = (New-Object PSCredential -ArgumentList '<LoginName>',(ConvertTo-SecureString -AsPlainText -Force '<Password>'))

Import-NAVEncryptionKey -ApplicationDatabaseCredentials $Credential -ApplicationDatabaseServer <DatabaseServer> -ApplicationDatabaseName <DatabaseName> -KeyPath '<KeyFilePath>' -ServerInstance <InstanceName> -Force

Did my solution solve your problem? Leave a reply.

Dynamics NAV HOW TO Microsoft PowerShell Registry

HOW TO set NAV Service Startup Type via PowerShell

When you create a new Instance using the command New-NAVServerInstance it will set the Startup Type of the NAV Service as “Automatic” by default.

I haven’t found any NAV command to change it, so let us think about NAV Service as any other Windows Service.

In PowerShell there is a command Set-Service that allows to change an existing Service.

NAV Service names have the following format


where <ServerInstance> is the name of the NAV Instance

So running the following command we can change the Startup Type to Automatic, Manual or Disabled.
[sourcecode lang=”powershell”]Set-Service –Name "MicrosoftDynamicsNavServer`$DynamicsNAV100" –StartupType Manual[/sourcecode]
This example will set the NAV Service for the Server Instance DynamicsNAV100 to Manual.

Unfortunately -StartupType parameter does not have a value for Automatic (Delayed Start)

So it seams a bit tricky but just looking into the register I’ve noted that the other services that had Automatic (Delayed Start) as Startup Type have a REG_DWORD value called “DelayedAutoStart” with value 1 under HKLM\System\CurrentControlSet\Services\

So the idea is use the previous command to set the Startup Type to Automatic then add REG_DWORD via script
[sourcecode lang=”powershell”]Set-Service –Name "MicrosoftDynamicsNavServer`$DynamicsNAV100" –StartupType Automatic
Set-ItemProperty -Path "Registry::HKLM\System\CurrentControlSet\Services\MicrosoftDynamicsNavServer`$DynamicsNAV100" -Name "DelayedAutostart" -Value 1 -Type DWORD[/sourcecode]

In reality we could setup everything from the registry
[sourcecode lang=”powershell”]Set-ItemProperty -Path "Registry::HKLM\System\CurrentControlSet\Services\MicrosoftDynamicsNavServer`$DynamicsNAV100" -Name "Start" -Value 2 -Type DWORD
Set-ItemProperty -Path "Registry::HKLM\System\CurrentControlSet\Services\MicrosoftDynamicsNavServer`$DynamicsNAV100" -Name "DelayedAutostart" -Value 1 -Type DWORD[/sourcecode]

NOTE 1: Both scripts will work only if the current Startup Type is different of Automatic
NOTE 2: “DelayedAutostart” DWORD will have effect only if you setup the Service Type as Automatic.

Did my HOW TO help you? Leave a reply.

%d bloggers like this: