Load NAV Service DLL for a specific Server Instance

Sometimes, you have multiple NAV Instances installed. When you have multiple installations, with different builds, you cannot use the PowerShell functions for each instance.

You could try loading the NavAdminTool.PS1 in the correct NST folder. However, this will probably not even work.

I have created a PowerShell function for this. This function was actually already posted in this post. But after receiving some feedback, about the title, I decided to create a separate post about it.

After editing the NavAdminTool.PS1, I found this:

$nstPath = "HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV\80\Service"
$managementDllPath = Join-Path (Get-ItemProperty -path $nstPath).Path '\Microsoft.Dynamics.Nav.Management.dll'
# First try to import the management module
Import-Module $managementDllPath -ErrorVariable errorVariable -ErrorAction SilentlyContinue

The PowerShell module will always read the registry for the installed version, and take that DLL…
So as we can read a bit further into the script, we can just Import-Module of the DLL directly. You will only not get a list of the imported commands.

Import-Module PATHTOMYNSTFOLDER\Microsoft.Dynamics.Nav.Management.dll

Or, if you want to be a bit more easy, you can you the function I created with a fellow colleague below:

function Load-NAVServiceDLL
    param([string] $ServerInstance)
    Remove-Module Microsoft.Dynamics.Nav.Management -Force -ErrorAction SilentlyContinue    

    $path = ([string](Get-WmiObject win32_service | ?{$_.Name.ToString().ToUpper() -like "*NavServer*$ServerInstance*"} | select PathName).PathName).ToUpper()
    $shortPath = $path.Substring(0,$path.IndexOf("EXE") + 3)
    if ($shortPath.StartsWith('"'))
        $shortPath = $shortPath.Remove(0,1)

    $PowerShellDLL = (Get-ChildItem -Path ((Get-ChildItem $ShortPath).Directory.FullName) "Microsoft.Dynamics.Nav.Management.DLL").FullName
    write-host "`nWelcome to the Server Admin Tool Shell!"
    write-host "For a complete list of Server cmdlets type`n"

    # Register Microsoft Dynamics NAV Management DLL
    Import-Module $PowerShellDLL -Force 

    write-host -fore Yellow "Get-Command -Module Microsoft.Dynamics.Nav.Management`n"

    # Print available commands
    Get-Command -Module Microsoft.Dynamics.Nav.Management 

This function can be used with partial NST names:

Load-NAVServiceDLL -ServerInstance PartOfInstance

1 side node though. You cannot run the Import-Module multiple times for the same module (or different version) during the same session. It appears that PowerShell keeps using the first imported version, and ignoring the latest you actually selected.

9 thoughts on “Load NAV Service DLL for a specific Server Instance

  1. > It appears that PowerShell keeps using the first imported version, and ignoring the latest you actually selected

    Maybe if you use Import-Module’s -Force parameter?…

    • Hello Jan,

      Actually I have tried that, but for some reason, it still keeps using the first loaded version.
      This is sort of what I tried:
      Import-Module versionX -Force
      Get-Command somenavcommand => give the version X
      Remove-Module VersionX -Force
      Get-Command somenavcommand => no commands found (looks ok here)
      Import-Module versionY -Force
      Get-Command somenavcommand => give the version X

      Apparently, this has something to do with .NET and the assemblies being loading into the current AppDomain.
      The only option would be to open another session of powershell and exiting the current one.

      • Makes sense.

        I don’t now if it would work in this particular case, but I think the way to deal with the challenge you mentioned is normally creating a new PowerShell instance from your current instance, then doing all the work (including the Import-Module) in that new instance, thus leaving the original instance in its pristine state. But I’m guessing you were probably already aware of that approach…

        • To be honest, I’m far from an advanced or experienced PowerShell developer. I know my basic way around it though.

          As for this script. Most of the times, I only need to execute some commands against a single instance, which never gives me any trouble.

          But you are right, for each different build, you should start a new PowerShell instance and run the things to do in that instance.

  2. Pingback: Get Session Count over multiple NST’s - Microsoft Dynamics NAV Community

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.