Saturday, April 25, 2020

Business Central Docker Containers: Make sure to update NavContainerHelper

By Steve Endow

If you are using Business Central Docker containers, make sure to regularly update NAV Container Helper.


===============================
UPDATE:  After thinking about this more, it just didn't make sense that the NavContainerHelper module I had downloaded just 9 days ago would have an old reference to $NAV instead of BC.  The $NAV parameter would presumably have been phased out months ago.

So how was it possible that calling my 9 day old version of NavContainerHelper resulted in an error due to an old $NAV reference?

The explanation lies in my complete ignorance of PowerShell and how PowerShell modules work.

AJ Kauffmann explained to me that I can check which versions of the NavContainerHelper module I have installed.  This is probably obvious to PS folks, but I didn't know that PS maintained multiple module versions.

AJ informed me of the "-allversions" parameter that will show all of the versions I have installed.  I've collected quite a few.

I'm a Collector

I then started wondering:  Is it possible that PowerShell called one of these old versions, which did contain the NAV reference?

The answer is:  YES.  It is possible, and likely explains the error I encountered.



Here is a good article explaining the import module process:
Which Version Does Import-Module Import? 
Written by June Blender 
https://info.sapien.com/index.php/scripting/scripting-modules/which-version-does-import-module-import

After reading this article, I also learned that modules are apparently not globally accessible in PS sessions--they have to be explicitly or implicitly loaded.  I didn't realize that either, and is something I need to research further to understand how that works.


Based on this, I think I have now learned several lessons.

1. Make sure to regularly update your PS modules

2. Make sure to regularly clean out old PS modules if you don't need them  (AJ helpfully pointed me to this article on removing old modules)

3. Never assume that the latest version of a module is being used in a given PS session. Perhaps explicitly import specific modules to ensure you are loading the correct version?  I don't yet know enough about PS to know the best practices around this last one.

I'm open to any suggestions or articles on best practices with maintaining and importing PS modules.  I clearly have much to learn.

Jonathan Cox shared this PowerShell doc page about PS Profiles, a feature that might be useful in minimizing these issues:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles

===============================


I was trying to create a new user in one of my Business Central containers using this PowerShell script.

$navcredential = New-Object System.Management.Automation.PSCredential -argumentList "admin", (ConvertTo-SecureString -String "P@ssword1" -AsPlainText -Force)


New-NavContainerNavUser -containerName "aldev" -Credential $navcredential 


But when I ran the script, I got this error.


New-NavContainerNavUser -containerName "aldev1" -Credential $navcredential
Creating NAV User admin
Unable to locate the server instance: MicrosoftDynamicsNavServer$NAV
    + CategoryInfo          : NotSpecified: (0:Int32) [New-NAVServerUser], NavCommandException
    + FullyQualifiedErrorId : MicrosoftDynamicsNavServer$NAV/default,Microsoft.Dynamics.Nav.Management.Cmdlets.NewNavServerUser
    + PSComputerName        : 30bb13cf557da9cba6a61270dc47c49baa2eae70490387656de6698d5bcbf7bc



The mention of $NAV in the error was pretty odd, as that should be BC with the current Docker sandboxes.

AJ Kauffmann recommended that I try updating NAV Container Helper in case I had an older version.

I thought I had updated NAV Container Helper relatively recently, as my newest Docker image is 9 days old, but I updated it this morning.

Sure enough, that resolved my issue.  I am puzzled about what version of NAV Container Helper I had (unfortunately I didn't check the version before I updated) and how I had one so old that the New User script failed, but it's a simple fix to run the update process.

I copied these commands from my main NAV Container Helper script to perform the update:

Install-module navcontainerhelper -Force


Import-Module navcontainerhelper 


But apparently this line will to update the script as well:

Update-Module navcontainerhelper -Force


After the update, you can check the version of NAV Container Helper with this command:

Get-InstalledModule -Name navcontainerhelper 


Version 0.6.5.6 as of April 24, 2020


UPDATE:  As noted above, make sure to review your module versions using the "-allversions" paramter and clean out old versions that you don't need, or find a technique that ensures that you are using the correct module version for your script.


Steve Endow is a Microsoft MVP in Los Angeles.  He works with Dynamics 365 Business Central, Power Automate, Power Apps, Azure, Dynamics GP, SQL Server, and .NET

You can also find him on Twitter and YouTube

No comments:

Post a Comment

All comments must be reviewed and approved before being published. Your comment will not appear immediately.

Learning Business Central Development from Scratch Video Series: Episode 1 and 2

By Steve Endow If you are new to Dynamics 365 Business Central development, join me on a journey to learn how to develop Business Central Ex...