Using Windows Powershell cmdlets to modify companies and databases


Have you been following the coffee break series of blog posts recently? Are you ready to start using Windows PowerShell? If you do not want to get started on your own development database, how about using the Microsoft Dynamics NAV 2015 demonstration database? In this blogpost, we use Windows PowerShell to change the demonstration database into a multitenant environment. Not because we think you should make your own solution multitenant, but because it makes it easier to see what is really going on.

If we look at the Demo Database NAV (8-0) database which is provided on the product DVD (W1), it has one company called CRONUS International Ltd. The first step towards multitenancy is to create additional companies. For this small task we will use the Windows PowerShell ISE. On your Windows machine, you will need to start the Windows PowerShell ISE in an elevated command prompt (Run as Administrator). After that you will need to set the execution policy. You can read more about this here. After that, we will show the cmdlets that are needed and the modules you need to import.

Since we will only run scripts that change the demonstration database, we will run the following command:

Set-ExecutionPolicy unrestricted

After each command, you must press F5. Now choose File in the menu and then select New to open a new screen or simply press CTRL+N. Type in the following command:

Import-Module ‘C:\Program Files\Microsoft Dynamics NAV\80\Service\NavAdminTool.ps1’

Press F5, CTRL+N. Now we need to create some new companies.

Copy-NAVCompany -DestinationCompanyName ‘CRONUS Production Ltd.’ –SourceCompanyName ‘CRONUS International Ltd.’ -ServerInstance DynamicsNAV80W1
Copy-NAVCompany -DestinationCompanyName ‘CRONUS Sales Ltd.’ -SourceCompanyName ‘CRONUS International Ltd.’ -ServerInstance DynamicsNAV80W1
Copy-NAVCompany -DestinationCompanyName ‘CRONUS Marketing Ltd.’ -SourceCompanyName ‘CRONUS International Ltd.’-ServerInstance DynamicsNAV80W1
Copy-NAVCompany -DestinationCompanyName ‘CRONUS Staff Ltd.’ -SourceCompanyName ‘CRONUS International Ltd.’ -ServerInstance DynamicsNAV80W1

Press F5, CTRL+N. Now we will export the application data into a new database.

Export-NAVApplication –DatabaseServer SQL\INSTANCE –DatabaseName ‘Demo Database NAV (8-0)’ –DestinationDatabaseName ‘Demo Database NAV (8-0) App’ –Force

NOTE: Change the value of the DatabaseServer parameter to specify your server name and SQL Server instance that is hosting the Demo Database NAV (8-0) database.

Press F5, CTRL+N. Now we will prepare the service tier instance

Set-NavServerInstance DynamicsNAV80W1 –Stop
Set-NAVServerConfiguration -ServerInstance DynamicsNAV80W1 -Keyname MultiTenant  -KeyValue “True”
Set-NAVServerConfiguration -ServerInstance DynamicsNAV80W1 -Keyname DatabaseName -KeyValue “”
Set-NavServerInstance DynamicsNAV80W1 –Start

Press F5, CTRL+N. Now we will mount the application and first tenant

Mount-NAVApplication -DatabaseServer SQL\INSTANCE -DatabaseName ‘Demo Database NAV (8-0) App’ -ServerInstance DynamicsNAV80W1
Mount-NAVTenant -ServerInstance DynamicsNAV80W1 -Id Default –DatabaseName ‘Demo Database NAV (8-0)’ -OverWriteTenantIDInDatabase –AllowAppDatabaseWrite

NOTE: In a Microsoft Dynamics NAV application that is used in a multitenant deployment, some areas require you to set up web services. Since web services are created in the application database, you must create at least one tenant that has write access to the application database. This setting is determined by the Allow application database writes parameter when you mount a tenant against a Microsoft Dynamics NAV Server instance. For more information, see How to: Mount or Dismount a Tenant on a Microsoft Dynamics Server Instance.

For example, you can create a dedicated administration tenant that you mount against the Microsoft Dynamics NAV Server instance when you create web services for an application. It is also needed allowing to importing FOB files. For more information see this earlier blog post.

Press F5, CTRL+N. Now we will import the example Windows PowerShell scripts that you can find on the Microsoft Dynamics NAV 2015 product media:

Import-Module ‘D:\NAV.8.0.39663.W1.DVD\WindowsPowerShellScripts\NAVMultitenancySamples.psm1’

NOTE: You can copy the WindowsPowerShellScripts folder from the product media to a safe location on your hard drive as long as you do copy all files in that folder
NOTE: If you are running the PowerShell scripts on a computer that does not have SQL Server / SQL Server tools installed, the following tools are necessary: Microsoft® Windows PowerShell Extensions for Microsoft® SQL Server® 2012

Press F5, CTRL+N and we will move the companies from the default tenant to newly created tenants.

HowTo-MoveCompanyToTenant -ServerInstance DynamicsNAV80W1 –FromDatabase ‘Demo Database NAV (8-0)’ -OldTenantName Default -NewTenantName Production -CompanyName ‘CRONUS Production Ltd.’ -ToDatabase Production -DatabaseServer SQL\Instance

HowTo-MoveCompanyToTenant -ServerInstance DynamicsNAV80W1 –FromDatabase ‘Demo Database NAV (8-0)’ -OldTenantName Default –NewTenantName Sales -CompanyName ‘CRONUS Sales Ltd.’ –ToDatabase Sales -DatabaseServer SQL\Instance

HowTo-MoveCompanyToTenant -ServerInstance DynamicsNAV80W1 –FromDatabase ‘Demo Database NAV (8-0)’ -OldTenantName Default -NewTenantName Marketing -CompanyName ‘CRONUS Marketing Ltd.’ -ToDatabase Marketing -DatabaseServer SQL\Instance

HowTo-MoveCompanyToTenant -ServerInstance DynamicsNAV80W1 –FromDatabase ‘Demo Database NAV (8-0)’ -OldTenantName Default –NewTenantName Staff -CompanyName ‘CRONUS Staff Ltd.’ –ToDatabase Staff –DatabaseServer SQL\Instance

Press F5, CTRL+N and we will now remove the companies from the default tenant.

Remove-NAVCompany -Tenant Default -CompanyName ‘CRONUS Production Ltd.’ -ServerInstance DynamicsNAV80W1
Remove-NAVCompany -Tenant Default -CompanyName ‘CRONUS Sales Ltd.’ -ServerInstance DynamicsNAV80W1
Remove-NAVCompany -Tenant Default -CompanyName ‘CRONUS Marketing Ltd.’ -ServerInstance DynamicsNAV80W1
Remove-NAVCompany -Tenant Default -CompanyName ‘CRONUS Staff Ltd.’ -ServerInstance DynamicsNAV80W1

To dismount and mount again, I refer you to my previous posting here. You can simply start the Windows client and if you do not change the config files, you will get a message stating that no tenants were specified. Press OK and change the url like this:

name_servicetier:7046/DynamicsNAV80W1/default
name_servicetier:7046/DynamicsNAV80W1/production
name_servicetier:7046/DynamicsNAV80W1/sales
name_servicetier:7046/DynamicsNAV80W1/marketing
name_servicetier:7046/DynamicsNAV80W1/staff

NOTE: If you set up multitenancy on a machine that is configured with other releases of Dynamics NAV side by side, it could be that the Administration console may generate an error stating in the PowerShell ISE tool that you can only run the Powershell command lets for a specific release. The root cause could be that you installed Dynamics NAV 2013 R2 after you installed Dynamics NAV 2015 or vice versa. You would need to verify the following key in the registry. It should match the release you are using. V7.1 is the build number for Dynamics NAV 2013 R2 and v8.0 is the build number for Dynamics NAV 2015.


Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\Microsoft.Dynamics.Nav.Management]
“ApplicationBase”=”C:\\\\Program Files\\\\Microsoft Dynamics NAV\\\\71\\\\Service\\\\”
“AssemblyName”=”Microsoft.Dynamics.Nav.Management, Version=7.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35”
“Description”=”Microsoft Dynamics Nav Management Snap-in”
“ModuleName”=”C:\\Program Files\\Microsoft Dynamics NAV\\71\\Service\\Microsoft.Dynamics.Nav.Management.dll”
“PowerShellVersion”=”2.0”
“Vendor”=”Microsoft Corporation”
“Version”=”7.1.0.0”

 

Regards,

Marco Mels
CSS EMEA

This posting is provided “AS IS” with no warranties, and confers no rights

Comments (2)

  1. Jens Glathe says:

    A message from (my) reality: Powershell is bad. And a crutch, in the best of all cases. What's wrong with a Development Environment that actually works without the use of Powershell? Or with a backup/restore function that is as useful as the .fbk functionality was? My reality is: We don't have tenants, we don't have ERP on Azure. ERP data is too sensitive to put it into a public cloud. And I'm not thinking about the next million sales, more like the next 10. As some other commenter said last year: Start fixing the product for actual real-world use, and don't blog on least-priority stuff.

  2. Pallea says:

    And a Message from mine 😉 I just LOVE powershells! Automating and doing backups from command-prompts. Easy creation of users from a command-prompt, and not to forget doing code upgrades using a commandprompt. All stuff that are 100-times faster than doing it by hand.

    I also need Tennants and several of my customers would just love to run NAV in/on Azure.

    So everyone is entitled to have their opion, and also to disagree and I do very much disagree with you Jens. Sorry about that.

    If Microsoft stopped moving forward we would still run on a DOS-application and losing customers every single minute to other products – and that would be more than a shame as it would also mean that I would be out of business.

    Having said that, I still believe that people arguing about stuff not working, should move forward and start making lists of what is not working, and not just complain – but you knew I would say that 🙂

    Have a great weekend!

    Thanks

    Palle