ArcherPoint Dynamics NAV Business Central Developer Digest - vol 311 Suzanne Scanlan Fri, 09/18/2020 - 05:05

ArcherPoint Dynamics NAV / Business Central Developer Digest - Vol 311

The Dynamics NAV and Business Central community, including the ArcherPoint technical staff, is made up of developers, project managers, and consultants who are constantly communicating, with the common goal of  sharing helpful information with one another to help customers be more successful.

As they run into issues and questions, find the answers, and make new discoveries, they post them on blogs, forums, social everyone can benefit. We in Marketing watch these interactions and never cease to be amazed by the creativity, dedication, and brainpower we’re so fortunate to have in this community—so we thought, wouldn’t it be great to share this great information with everyone who might not have the time to check out the multitude of resources out there? So, the ArcherPoint Microsoft Dynamics NAV Developer Digest was born. Each week, we present a collection of thoughts and findings from NAV/BC experts and devotees around the world. We hope these insights will benefit you, too.

How Old is Your Code?

Kyle posed this question to the team: “Thought experiment for the day: Do you have any code still being used in production that is 10 years or older?” 

As you might guess, the answer is ‘Yes’ from many of our veteran developers on the ArcherPoint team. How about you? Do you have any code that is still in use over a decade later?

Useful PowerShell Commands: A Cheat Sheet

Tom Hunt created a cheat sheet of PowerShell commands: “I put a list of useful PowerShell commands together. I don't know if anyone else will find my cheat sheet useful, but it might help you understand things a little better.” 

PowerShell Commands

Stuff You See in Scripts 

  • The ` character is used to put multiple parameters for a command together in a script without having all of them on the same line, like:

        Invoke-NAVCodeunit `
          -ServerInstance $serverInstance `
          -CompanyName "CompanyName" `
          -CodeunitId 50107 `
          -MethodName "DeployAppEmailStart"

  • Those are all parameters sent to the Invoke-NAVCodeunit command.
  • Variable declaration: $variableName = “value” 
    • Variables can be passed as parameters into commands, like this:
      $theHelloVariable = “Hello World from Variable”
      Write-Host $theHelloVariable 

General PowerShell Commands

  • [command] -?
    • This should give you the list of parameters available for a command or cmdlet 
  • docker container ls
    • Gets current list of containers 
    • There’s a container ID that is different from the container name. The ID is generally what docker wants. The ID looks like “c8301e3e2d3e” or something like that; it’s a computer-generated key
  • docker restart [container ID] 
    • Restarts specified docker container
    • Use the container ID, not the container name
  • docker rm [container ID]
    • Deletes the specified docker container
  • Enter-PSSession (and Exit-PSSession)
    • Starts a remote PowerShell session on another computer.
    • Google the command for parameters here
    • Exit-PSSession is how you leave a session on another computer. 
  • For, ForEach, ForEach-Object, other For stuff
    • This is the For looping construct 
  • If ([condition test]) {[stuff if true]} else {[stuff if false]}
    • This is the If conditional construct
    • For the comparison operators, run Get-Help about_Comparison_Operators
      • PowerShell’s comparison operators are different from other languages. For example -eq is equal, -gt is greater than, and -lt is less than
    • Example:
      • if (2 + 2 -eq 4) {Write-Host "Math works correctly"} else {Write-Host "Math does not work."}
    • The If statement can also be broken up with ` onto multiple lines and with spaces to be more readable, like:
      • if (2 + 2 -eq 4) ` 
      •      {Write-Host "Math works correctly"} `
      • else ‘
      •      {Write-Host "Math does not work."}
  • Install-Module
    • Installs a PowerShell cmdlet from a central Microsoft repository. 
    • Running with the -AllowClobber parameter lets you override similar modules 
  • Select-String –Path [directory]\*.[file extension] –Pattern [string to find]
  • Write-Host “contents to write in double quotes”
    • Writes whatever you say to the console, like: Write-Host “Hello World”
    • When writing scripts, it’s a good idea to do this and say what you’re doing every few lines so you can see where you are when things stop working, or if something unexpectedly takes a while 

NAV-Specific PowerShell Commands

NOTE: These require NAVContainerHelper

  • az login –allow-no-subscriptions
    • This actually doesn’t require NAVContainerHelper, but it does grab the token for authentication and downloading artifacts
  • Get-NavContainerEventLog [container name]
    • Grabs the event log for the specified docker container and then displays it on the host system event viewer
  • Get-NAVServerSession -ServerInstance [instance name]
    • Lists the users connected to a Business Central server instance
  • Invoke-NAVCodeunit
    • Allows you to run a NAV codeunit from PowerShell 
    • Parameters:
      • -ServerInstance is the NAV server to use
      • -CompanyName is the NAV company. Surround it with double quotes to handle spaces
      • -CodeunitId is the codeunit number to run
      • -MethodName is the method to run 
  • Publish-NAVApp 
    • Publishes a NAV app file to a specific NAV server instance, like:
      • Publish-NAVApp -ServerInstance $ServerInstance -Path $appFile
  • Sync-NAVTenant 
    • Synchronizes a NAV tenant
    • Requires a parameter to indicate a server instance, like:
      • Sync-NAVTenant $ServerInstance -force
  • UnPublish-NAVApp
    • Removes a NAV app file from a specific NAV server instance, like:
      • UnPublish-NAVApp -ServerInstance $ServerInstance -Name $appName -version $oldVersion

If you are interested in Dynamics NAV and Business Central development, be sure to see our collection of NAV/BC Development Blogs.

Read the "How To" blogs from ArcherPoint for practical advice on using Microsoft Dynamics NAV and Dynamics 365 Business Central.

Blog tags