Using PowerShell to Refresh a Microsoft Dynamics Business Central On Prem Test Database Kyle Hardin Mon, 02/03/2020 - 05:38

How To Use PowerShell to Refresh a Microsoft Dynamics Business Central On Premises Test Database

",sans-serif">I don’t blog very often, but when I do, I try to include some sort of catchy image as part of the intro. A brief search of the Googles for the word, “refreshing” was disappointing; images ranged from fruity and fizzy boozy drinks to platitudes along the “Live Love Laugh” genre. I can’t work with either of those. So, straight to business.

",sans-serif">Stopping Service Tiers. SQL backups and restores. Fiddling with company names and red warning banners so users know they are in a Test database. While these steps might be super enjoyable due to their monotony and ease of missing an important step, I thought I’d try to put together a PowerShell script to streamline the process. I realize this removes all the fun.

",sans-serif">This article is only for on-premise installations of Business Central (BC). The cloud/SaaS version requires a completely different method of refreshing involving Sandboxes. The steps below might also work for older versions of NAV, but I have not done any regression testing.

",sans-serif">The basic steps for a Test database refresh are as follows:

  1. ",sans-serif">SQL backup of production
  2. ",sans-serif">Stop Test service tier
  3. ",sans-serif">SQL restore to Test
  4. ",sans-serif">Restart Test service tier
  5. ",sans-serif">Rename production company name to test company name
  6. ",sans-serif">Add a banner/warning that indicates a Test database

",sans-serif">All these steps can be done with PowerShell except for the last – adding a banner requires running some BC code to update Company Information.

",sans-serif">There are two dependencies that need to be installed in order for this to work. First is the Business Central Administration Shell, which is part of the Business Central Installation kit. That option is highlighted in the screen snap below:

",sans-serif">Figure 1 - Microsoft Dynamics Business Administration Shell

",sans-serif">Second is the SQL Server PowerShell Module. This can be safely installed on your Service Tier server without installing any of the other SQL tools. You don’t even need SQL Server Management Studio; the PowerShell module is stand-alone.

  • ",sans-serif">Open a PowerShell window as administrator
  • ",sans-serif">See if the SQL module is already installed

                                    ",sans-serif">get-module sqlserver -listavailable

  • ",sans-serif">If nothing shows up, you need to install it.

                                    ",sans-serif">install-module -name sqlserver

",sans-serif">Log on to your Service Tier server. Open the Business Central Administration Shell as an Administrator. I’m going to go step by step to describe what is going on, but I will include the full PowerShell script and AL code at the end of the article.

  1. ",sans-serif">SQL Backup of Production

",sans-serif">Full documentation for the Backup PowerShell command is here:

",sans-serif">https://docs.microsoft.com/en-us/powershell/module/sqlserver/backup-sqldatabase?view=sqlserver-ps

",sans-serif">Backup-SqlDatabase `

",sans-serif">  -ServerInstance "yoursqlserver.yourdomain.com" `

",sans-serif">  -Database "Business Central Production" `

",sans-serif">  -BackupFile "\\fileshareserver\fileshare\TestRestore.bak" `

",sans-serif">  -BackupAction Database `

",sans-serif">  -CopyOnly `

",sans-serif">  -CompressionOption on `

",sans-serif">  -Initialize

 

",sans-serif">The backup command will need to be edited to match your environment. The notable option is CopyOnly: this option is important because it will leave the SQL transaction log alone and won’t mess up any automated scheduled backups.

  1. ",sans-serif">Stop Test Service Tier

",sans-serif">Get-Service 'MicrosoftDynamicsNavServer$BCTest' `

",sans-serif">-ComputerName "test-service-tier.yourdomain.com" | Stop-Service

  1. ",sans-serif">SQL Restore to Test

",sans-serif">Full docs for the Restore command:

",sans-serif">https://docs.microsoft.com/en-us/powershell/module/sqlserver/restore-sqldatabase?view=sqlserver-ps

",sans-serif">Restore-SqlDatabase `

",sans-serif">  -ServerInstance "yoursqlserver.yourdomain.com" `

",sans-serif">  -Database "Business Central Test" `

",sans-serif">  -BackupFile "\\fileshareserver\fileshare\TestRestore.bak" `

",sans-serif">  -RestoreAction Database `

",sans-serif">  -ReplaceDatabase `

",sans-serif">  -Confirm

 

",sans-serif">I am huge fan of Confirm, even though most people will blast right past the prompt to get the default.

  1. ",sans-serif">Restart Test Service Tier

",sans-serif">Get-Service 'MicrosoftDynamicsNavServer$BCTest' `

",sans-serif">-ComputerName "test-service-tier.yourdomain.com" | Start-Service

 

  1. ",sans-serif">Rename Production Company Name to Test Company Name

",sans-serif">This takes two steps.

",sans-serif">Rename-NAVCompany `

",sans-serif">  -ServerInstance "BCTest" `

",sans-serif">  -CompanyName "BC Production" `

",sans-serif">  -NewCompanyName "Test BC Test" `

",sans-serif">  -Verbose

",sans-serif">Set-NAVCompany `

",sans-serif">  -ServerInstance "BCTest" `

",sans-serif">  -CompanyName "Test BC Test" `

",sans-serif">  -CompanyDisplayName "TEST BC TEST"

 

  1. ",sans-serif">Add a banner / warning that indicates a Test database

",sans-serif">I could not figure out a way to do this with PowerShell, since this step requires updating Company Information inside of BC. You can consider this an optional step, since BC 15 only allows a four-character Badge, so this may not be worth your effort. This is the AL codeunit, but this can be re-written in C/AL for older versions. Put this in its own Extension so it can be published as a stand-alone tool.

",sans-serif">codeunit 50107 "ARC PowerShell Tools"

",sans-serif">{

",sans-serif">    procedure SetTestCompanyBadge()

",sans-serif">    var

",sans-serif">        CompanyInfo: Record "Company Information";

",sans-serif">    begin

",sans-serif">        with CompanyInfo do begin

",sans-serif">            Get();

",sans-serif">            "System Indicator" := "System Indicator"::Custom;

",sans-serif">            "System Indicator Style" := "System Indicator Style"::Accent6;

",sans-serif">            "Custom System Indicator Text" := 'TEST';

",sans-serif">            Modify(true);

",sans-serif">        end;

",sans-serif">    end;

",sans-serif">}

 

",sans-serif">Call this codeunit from PowerShell:

 

",sans-serif">Invoke-NAVCodeunit `

",sans-serif">  -ServerInstance "BCTest" `

",sans-serif">  -CompanyName "Test BC Test" `

",sans-serif">  -CodeunitId 50107 `

",sans-serif">  -MethodName "SetTestCompanyBadge"

 

",sans-serif">This is the full PowerShell Script:

 

",sans-serif">Backup-SqlDatabase `

",sans-serif">  -ServerInstance "yoursqlserver.yourdomain.com" `

",sans-serif">  -Database "Business Central Production" `

",sans-serif">  -BackupFile "\\fileshareserver\fileshare\TestRestore.bak" `

",sans-serif">  -BackupAction Database `

",sans-serif">  -CopyOnly `

",sans-serif">  -CompressionOption on `

",sans-serif">  -Initialize

 

",sans-serif">Get-Service 'MicrosoftDynamicsNavServer$BCTest' `

",sans-serif">  -ComputerName "test-service-tier.yourdomain.com" | Stop-Service

 

",sans-serif">Restore-SqlDatabase `

",sans-serif">  -ServerInstance "yoursqlserver.yourdomain.com" `

",sans-serif">  -Database "Business Central Test" `

",sans-serif">  -BackupFile "\\fileshareserver\fileshare\TestRestore.bak" `

",sans-serif">  -RestoreAction Database `

",sans-serif">  -ReplaceDatabase `

",sans-serif">  -Confirm

 

",sans-serif">Get-Service 'MicrosoftDynamicsNavServer$BCTest' `

",sans-serif">  -ComputerName "test-service-tier.yourdomain.com" | Start-Service

 

",sans-serif">Rename-NAVCompany `

",sans-serif">  -ServerInstance "BCTest" `

",sans-serif">  -CompanyName "BC Production" `

",sans-serif">  -NewCompanyName "Test BC Test" `

",sans-serif">  -Verbose

",sans-serif">Set-NAVCompany `

",sans-serif">  -ServerInstance "BCTest" `

",sans-serif">  -CompanyName "Test BC Test" `

",sans-serif">  -CompanyDisplayName "TEST BC TEST"

 

",sans-serif">Invoke-NAVCodeunit `

",sans-serif">  -ServerInstance "BCTest" `

",sans-serif">  -CompanyName "Test BC Test" `

",sans-serif">  -CodeunitId 50107 `

",sans-serif">  -MethodName "SetTestCompanyBadge"

 

",sans-serif">If you have any questions about Dynamics NAV or Business Central questions for any version, ",sans-serif">contact ArcherPoint",sans-serif">.

",sans-serif">Read more "How To" blogs from ArcherPoint",sans-serif"> for practical advice on using Microsoft Dynamics Business Central or NAV.

",sans-serif">If you are interested in NAV/Business Central development, check out our ",sans-serif">collection of NAV Development Blogs.

Blog tags