Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Friday, July 24, 2020

Recording Business Central NavContainerHelper benchmarks

By Steve Endow

As I've been using NavContainerHelper, it's become clear that there can be a lot of waiting as it downloads gigabytes of data for the images and hundreds of megabytes for the artifacts.  It then has to mash all of that together to build a Business Central Container.  It can take a while.

Sometimes it seems the process is pretty quick.  But sometimes the process seems to take forever.  When it's in the middle of doing its thing, I can't tell whether it's taking longer than normal, or if it just feels like it's taking a long time.

So I'm now measuring how long it takes for NavContainerHelper to create containers.

Ya gotta measure things

If the container setup process takes longer than normal, I'll want to know why.  Was it a performance issue downloading images or artifacts?  Or is there a performance issue with the VM?  Or did some error cause the script to fail prematurely?


Here's the PowerShell script I've setup to record the start time, end time, and elapsed time of the New-BcContainer process.  This is a sample that uses the values for building a BC container in my environment--you'll want to adjust the New-BcContainer parameter values to your preferred values.


 $StartTime = $(get-date)   
  Try {   
   $output = "Started: " + $StartTime   
   Write-Output $output    
   $containerName = 'bc1'   
   $password = 'P@ssw0rd'   
   $securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force   
   $credential = New-Object pscredential 'admin', $securePassword   
   $auth = 'UserPassword'   
   $artifactUrl = Get-BcArtifactUrl -type 'Sandbox' -version '' -country 'us' -select 'Latest'   
   New-BcContainer `   
    -accept_eula `   
    -containerName $containerName `   
    -credential $credential `   
    -auth $auth `   
    -artifactUrl $artifactUrl `   
    -imageName 'myimage1' `   
    -dns '8.8.8.8' `   
    -updateHosts   
   }   
   Catch {   
    Write-Error "Um, something didn't go well:"   
    Write-Error $_   
   }   
   Finally {   
    $EndTime = $(get-date)   
    $output = "End time: " + $EndTime   
    Write-Output $output    
    $elapsedTime = $(get-date) - $StartTime   
    $totalTime = "{0:HH:mm:ss}" -f ([datetime]$elapsedTime.Ticks)   
    $output = "Elapsed time: " + $totalTime   
    Write-Output $output   
   }   


At the top of the script, I record the start time, then output that time in the log so that I can see when the script started.

I added a try/catch block so that even if the script fails, my script outputs the end time and elapsed time.

In the finally block, I record the time again, then calculate elapsed time.  The elapsed time is then output to the log.

Building a new named image vs. using an existing named image

In this example, I see that when the script had to build a new named image, it took 13 minutes to create the named image and then create the container.  But when it was able to use an existing named image, it only took 1 minute.

Good to know!

Now I can use this script to see how my dedicated physical server compares to my VMs.  I assume that the VMs will be slower, but I'm curious to see how much slower.


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

You can also find him on Twitter and YouTube

http://www.precipioservices.com



Automatically Updating NavContainerHelper

By Steve Endow

One of the challenges I've had using the Business Central NavContainerHelper module has been managing the versions and updates.  Freddy Kristiansen is constantly updating NavContainerHelper to add features or fix issues, so a new version might be released within 24 hours of the prior release.

It seems like there is at least one update per week.  As a result, I always forget to update NavContainerHelper and often end up using an older version, which can sometimes result in errors.

I wanted to automate the update process so that I always know that I have the latest version of NavContainerHelper installed.  And ONLY the latest version--making sure that I don't have any older versions installed side-by-side.

I don't know if this is the best method, but I think it should work.  If you know of a better way, let me know!

Here's the script I'd like to run automatically every day:

 $MyModuleName = 'NavContainerHelper'  
 Uninstall-Module $MyModuleName -Force -AllVersions  
 Install-Module $MyModuleName -Force  

The Script

I want to install the latest version, but I want to make sure that I don't have an older version still installed.  There are a few ways to do this, but I figure with this script, it removes all existing versions, and then installs the latest version.

I normally don't recommend Install-Module -Force if you are running the script manually, but in this case, I believe I need -Force in order to bypass the verification dialog that appears during installation.  Also, since I know that I've just uninstalled all versions before installing the latest version, I think using -Force should be okay in this particular case.

I saved the above 3 line script to a .ps1 file.

I then opened Windows Task Scheduler using Run As Administrator.

Windows Task Scheduler
I created a new folder called Steve to make it easier to find my tasks.

I then created a new task (not a Basic task).

Task - General Info

I gave the task a name.  Then I set the task to use the Local Administrator user.  This is on one of my dev VMs, and I know the PowerShell script will need Admin rights, so I chose Administrator just to avoid any hassles.  If you are on a company network / domain, you'll need to figure out which domain user / rights you need to allow the script to run as a task.

Nightly update
On the Triggers tab, I setup a schedule to run Daily at 8pm.

On the Actions tab, I created an action to call powershell with the -File argument, based on this blog post.



The argument I used looks like:

     -File C:\BCPowerShell\UpdateNavContainerHelper_v1.ps1


I then saved the task and tested it. 

It seems to work, so I'll keep an eye on it for a week or so to verify that NavContainerHelper is always on the latest version and that I don't have any old versions installed side-by-side.


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

You can also find him on Twitter and YouTube

http://www.precipioservices.com

Wednesday, July 22, 2020

Basic Business Central Docker Commands That I Use

By Steve Endow

On July 1, 2020, I made a video showing the basic Docker commands that I use to manage Business Central Docker Containers.

PowerShell isn't too bad

Here is a complete list of the commands that I've collected to manage Business Central Containers, as of July 2020.  If you have any others that you recommend, please comment below and let me know!


Thanks to Daniel and Tanya and D┼żoka for some additions!


   
 Open Powershell / Powershell ISE on HOST machine using Run As Administrator  
   
 
 NOTE: When referring to a Container ID or Image ID, you can use the first 
       few characters of the ID if they are unique--you do not have to always
       use the full ID.


 Check NavContainerHelper Version  
   
      Get-InstalledModule NavContainerHelper  
   
   
 Fresh Install of NavContainerHelper  
   
      Install-module navcontainerhelper  
      Import-Module navcontainerhelper  
   
   
 Install Latest NavContainerHelper  
   
 If you KNOW you don't have multiple versions installed:  
   
      Update-Module NavContainerHelper -Force  
   
 If you MIGHT have multiple versions installed:  
   
      $MyModuleName = <modulename>  
      Uninstall-Module $MyModuleName -allversions   
      Install-Module $MyModuleName
   
      NOTE: I do not recommend using the -Force option with Install-Module. 
            It can bypass valuable warnings that you want to know about.


 Alternate method to Remove Old Versions of NavContainerHelper  
   
      $ModuleName = 'navcontainerhelper';  
      $Latest = Get-InstalledModule $ModuleName;   
      Get-InstalledModule $ModuleName -AllVersions | ? {$_.Version -ne $Latest.Version} | Uninstall-Module -WhatIf  
   
   
   
 Container Management Commands  
   
      docker ps  
      docker ps -a  
        
      docker start <id>  
      docker stop <id>  
   
      
      docker container inspect 


 Remove Container  
   
    Recommended method:

      Remove-BCContainer <containername>


    Manual Docker method:

      docker stop <id> 
      docker rm <id> 
      Manually clean up hosts file
      Manually remove desktop icons
      Manually remove extensions from C:\ProgramData\NavContainerHelper\Extensions\
   

 Stop all running containers:  
        
      docker stop $(docker ps -aq)  
   
   
 Images  
   
       docker images  
       docker images -a  
   
 Remove Image  
   
      docker rmi <id>  
        
      docker rmi <repository>:<tag>  
   
   Remove all images:

      docker rmi $(docker images -aq)

   
 Clean up old images  
   
   Remove orphaned images with "< none >" identifiers:

      docker images -q -f dangling=true | % { docker rmi $_ }  


   Remove orphaned images:
   
   	  docker image prune -f
      
   Remove orphaned AND UNUSED images:
   
   WARNING: Be careful with this: It will remove generic OS images used by NavContainerHelper!

      docker image prune -a -f
   

   
 Get Artifact Info for an existing BC Container:  
   
      Get-BcContainerArtifactUrl containername  
        
      Get-NavContainerArtifactUrl containername  
   
   
 Diagnostics  
   
      Get-BcContainerEventLog  
   
   
   
 Create New BC Container  
   
      New-BcContainerWizard  
   
   
 https://blog.baudson.de/blog/stop-and-remove-all-docker-containers-and-images  
   




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

You can also find him on Twitter and YouTube

http://www.precipioservices.com

Wednesday, July 8, 2020

I'm no PowerShell expert: Discovering Update-Module for NavContainerHelper

By Steve Endow

I wrote a post in April, "Make sure to update NavContainerHelper", in which I described some issues I had that were caused by having several old versions of the NavContainerHelper module installed.

Today, while updating the NavContainerHelper module on my Docker VM, I learned a few things, and I think I finally have a basic understanding of the PowerShell Module install and update process.

If you're a PowerShell pro or a NavContainerHelper pro, this will probably seem obvious, but as a newbie with both tools, it was a "learning moment" for me that explained the issues I had previously with multiple NavContainerHelper versions.

Let's start with a screen shot.

Install-Module -Force Warning

Wednesday, July 1, 2020

Business Central Basic Docker Commands

By Steve Endow

If you haven't used Docker before, and if you're not a PowerShell expert, welcome to the club.

I am new to both, and the only reason I'm using Docker and PowerShell is to use Business Central Docker Containers.  I know just enough to do the bare minimum that I need to manage a few Business Central Docker Containers for development and testing.

I have a live stream video demonstrating the commands:

https://www.youtube.com/watch?v=u-mlyIqNl5c





Here are the basic Docker commands that I have been using.


 #Containers  
   
 docker ps  
 docker ps -a  
   
 docker start <id>  
 docker stop <id>  
   
 docker rm <id>  
   
 #Stop all running containers:  
 docker stop $(docker ps -aq)  
   
   
 #Images  
   
 docker images  
 docker images -a  
   
 docker rmi <id>  
   
 docker rmi <repository>:<tag>  
   
   
 #Check NavContainerHelper version  
 Get-InstalledModule  
   
   
 #Install Latest NavContainerHelper  
 $MyModuleName = <modulename>  
 Uninstall-Module $MyModuleName -allversions   
 Install-Module $MyModuleName -Force  





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

You can also find him on Twitter and YouTube


Fully automate the Business Central Container build process

By Steve Endow I don't like maintenance tasks.  I don't like having to remember to do something on a regular basis.  I don't l...