·
5 min read

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