In order to work with NAV containers, I highly recommend using NAVContainerHelper PowerShell module. In the version 0.5.0.0 a new really powerfull function Invoke-ScriptInNavContainer has been released, it allows you to invoke a PowerShell ScriptBlock in a NAV container.
Using NAVContainerHelper you do not have to care about loading NAV modules because they are already part of it and loaded, so to return to the title of this post, the script to synchronize database schema is:
Invoke-ScriptInNavContainer has two mandatory parameters, the name of the container and the script block. In the ScriptBlock, I’m using two standard PowerShell functions included in the NAV module Microsoft.Dynamics.Nav.Management:
This post actually started with an Issue I created in NavContainerHelper GitHub.
💡 I’m working in a script for Continuous Integration and although the creation of a container requires a few minutes I was looking at how to optimize the time of execution of the whole build process.
Freddy Kristiansen is a Microsoft Evangelist that is working hard to make it easier to work with NAV using Docker Containers, thanks again, Freddy.
Freddy initially marked my issue with label “WONTFIX” 🙁 but after I explained to him my motivation he changed the label to “ENHANCEMENT” 😀
He did not include any new functionality to NavContainerHelper but he kindly provided me with the script to copy and paste, and as I always do, I like to share good things.
Solution: Right after the creation of a NAV Container, run the following script:
# To add just after the creation of the NAV Container
$containerName = "scadev"
$config = Get-NavContainerServerConfiguration -ContainerName $containerName
Invoke-ScriptInNavContainer -containerName $containerName -scriptblock {
Param($DatabaseServer, $DatabaseInstance, $DatabaseName, $NewDatabaseName)
Copy-navDatabase -DatabaseServer $DatabaseServer -DatabaseInstance
$DatabaseInstance -SourceDatabaseName $DatabaseName -DestinationDatabaseName
$NewDatabaseName
} -argumentList $config.DatabaseServer, $config.DatabaseInstance, $config.DatabaseName, "backup"
So next time you run the build process, just verify that the Container exists and in case replace the database with the backup taken previously (it takes 10-20 seconds)