Tuesday 27 October 2015

Few Helpful PowerShell Commands which you can use for Upgrade Process in Navision 2016 – Part 2

You can use Windows PowerShell scripts to upgrade the latest version of Microsoft Dynamics NAV. Microsoft Dynamics NAV 2016 provides sample scripts that you can adapt for your deployment architecture.

Automating the Upgrade Process

When you upgrade to Microsoft Dynamics NAV 2016, you must first upgrade the application code, and then you upgrade the data.

In my earlier post I have explained this using PowerShell commands, you can find the link here: Helpful PowerShell Commands which you can use for Upgrade Process in Navision 2016

By using Windows PowerShell, you can automate both parts of the upgrade process. Also, you can use the same scripts to test each step in your upgrade process before you upgrade production databases.

You can combine this automated upgrade with a migration to multitenancy this makes maintenance easier for you.

The Sample Scripts for Code Upgrade

Microsoft Dynamics NAV includes sample scripts that illustrate how you can use Windows PowerShell cmdlets to upgrade your application to the latest version of Microsoft Dynamics NAV.

The sample scripts are located in the ApplicationMergeUtilities folder under the WindowsPowerShellScripts folder on the Microsoft Dynamics NAV product media.

However you can follow above post link steps explanation to get it done.

The Sample Scripts for Data Upgrade

Microsoft Dynamics NAV includes sample scripts that illustrate how you can automate the upgrade of data to the latest version of Microsoft Dynamics NAV.

The sample scripts are located in the Upgrade folder under the WindowsPowerShellScripts folder on the Microsoft Dynamics NAV product media. You can run the sample script using a partner license or a customer license.
PowerShell-3

To learn and follow MS suggested steps you can find details using this link. Automating the Upgrade Process using Windows PowerShell Scripts in Microsoft Dynamics NAV 2016

To run the sample script for the data upgrade of a Microsoft Dynamics NAV database, you must have a Microsoft Dynamics NAV 2013, Microsoft Dynamics NAV 2013 R2, or Microsoft Dynamics NAV 2015 database that is available on a SQL Server instance and is ready to be upgraded.

Here I present my version derived from above Steps:

To continue we will do some setup. Copy the Upgrade folder from above path and save as DataUpgradePSKit.

PowerShell-4

Create Folder OriginalScript and move the PS1 file on root to this folder although we don’t require for this exercise but you can safe copy for your reference. (Example, Set-PartnerSettings, Set-PowerShellEnvironment)

Create Backup folder, script will use to store backup of the database previous to start Upgrade process.

Create Upgrade Folder and place these files:

  • License File

  • New Merged Objects fob

  • Upgrade Toolkit / or your own prepared Upgrade Codeunits


PowerShell-5

  • Create ProcessLogs Folder, which will be used for recording log of Shell Script.


Here is the script which we will be using to perform our Data Upgrade process:

You can find this script here http://1drv.ms/1NyolVV or you can download from Menu of my Blog using Link Shared Files.

 

# Added below parameter values globally for ease of maintenance

# You just do correction on values here (as per your environment) and will be in effect for rest of below script

# No need to scan and change every occurrence for same value in different steps of the script.

# Select this section and Execute first so that these Variables value are available for rest of the script.
Import-Module 'C:\Program Files\Microsoft Dynamics NAV\90\Service\NavAdminTool.ps1'

$NAVUpgrade_NAVServerInstance = "UpgradedDBfrom2013R2"

$NAVUpgrade_NAVServerServiceAccount = "NT AUTHORITY\NETWORK SERVICE"

$NAVUpgrade_FinSqlExeFile = "C:\Program Files (x86)\Microsoft Dynamics NAV\90\RoleTailored Client\finsql.exe"

$NAVUpgrade_IDEModulePath = ""

$NAVUpgrade_DatabaseServer = "INDEL-AXT5283VM"

$NAVUpgrade_DatabaseInstance = ""

$NAVUpgrade_DatabaseName = "Demo Database NAV (7-1)"

$NAVUpgrade_DatabaseToUpgradeBakFile = "C:\UserData\DataUpgradePSKit\Backup\DynamicsNAV70_BeforeUpgrade.bak"

$NAVUpgrade_NewVersionObjectsFobFilePath = "C:\UserData\DataUpgradePSKit\Upgrade\NewObjects.fob"

$NAVUpgrade_UpgradeToolkitObjectsFobFilePath = "C:\UserData\DataUpgradePSKit\Upgrade\Upgrade710900.FOB"

$NAVUpgrade_UpgradeObjectsFilter = "Version List=UPGTK9.00.00"

$NAVUpgrade_UpgradeLogsDirectory = "C:\UserData\DataUpgradePSKit\Upgrade\ProcessLogs"

#$NAVUpgrade_RapidStartPackageFile = 'C:\UserData\DataUpgradePSKit\Upgrade\PackageSTCODES.rapidstart'

$NAVUpgrade_CurrentVersionLicenseFile = "C:\UserData\DataUpgradePSKit\Upgrade\DevLicense.flf"

$NAVUpgrade_PreviousVersionLicenseFilePath = "C:\UserData\DataUpgradePSKit\Upgrade\DevLicense.flf"

 

# Upgrade Steps:
Import-Module (Join-Path (Get-Location) 'Cmdlets\NAVUpgradeCmdlets.psm1') -DisableNameChecking

#1. Prepares the Windows PowerShell session by importing the required modules.
        # Import the NAV IDE Module.

Import-NAVIdeModule -IDEModuleSuggestedPath $NAVUpgrade_IDEModulePath -FinSqlExeFile $NAVUpgrade_FinSqlExeFile

Import-NAVManagementModule

Import-SqlPsModule

 

#2. Saves the current license from the Microsoft Dynamics NAV 2013, Microsoft Dynamics NAV 2013 R2, or Microsoft Dynamics NAV 2015 database.

# Backup current license from the application part of the database (table '$ndo$dbproperty') , if it exists
        Export-NAVLicenseFromApplicationDatabase `

-DatabaseName $NAVUpgrade_DatabaseName `

-DatabaseServer $NAVUpgrade_DatabaseServer `

-DatabaseInstance $NAVUpgrade_DatabaseInstance `

-LicenseFilePath $NAVUpgrade_PreviousVersionLicenseFilePath

 

#3. Creates a backup of the Microsoft Dynamics NAV 2013, Microsoft Dynamics NAV 2013 R2, or Microsoft Dynamics NAV 2015 database, and then converts the database to Microsoft Dynamics NAV 2016.
        Backup-NAVSqlDatabase `

-DatabaseServer $NAVUpgrade_DatabaseServer `

-DatabaseInstance $NAVUpgrade_DatabaseInstance `

-DatabaseName $NAVUpgrade_DatabaseName `

-DatabaseBackupFilePath $NAVUpgrade_DatabaseToUpgradeBakFile

$NAVUpgrade_DatabaseSQLServerInstance = Get-SqlServerInstance -DatabaseServer $NAVUpgrade_DatabaseServer -DatabaseInstance $NAVUpgrade_DatabaseInstance

$NavServerInfo = New-Object PSObject

Add-Member -InputObject $NavServerInfo -MemberType NoteProperty -Name NavServerName -Value "$NAVUpgrade_DatabaseServer"

Add-Member -InputObject $NavServerInfo -MemberType NoteProperty -Name NavServerInstance -Value (Get-NAVServerConfigurationValue  -ServerInstance $NAVUpgrade_NAVServerInstance -ConfigKeyName "ServerInstance")

Add-Member -InputObject $NavServerInfo -MemberType NoteProperty -Name NavServerManagementPort -Value (Get-NAVServerConfigurationValue -ServerInstance $NAVUpgrade_NAVServerInstance -ConfigKeyName "ManagementServicesPort")

 

# Perform technical upgrade of the NAV database
        Invoke-NAVDatabaseConversion `

-DatabaseName $NAVUpgrade_DatabaseName `

-DatabaseServer $NAVUpgrade_DatabaseSQLServerInstance `

-LogPath $NAVUpgrade_UpgradeLogsDirectory\"Database Conversion"

 

#4. Connects the Microsoft Dynamics NAV 2016 Server instance to the converted database, imports the Microsoft Dynamics NAV 2016 license file, and then synchronizes the table schema.

 

# Connect the NAV Server to the NAV database
        Connect-NAVServerToNAVDatabase  `

-NAVServerInstance $NAVUpgrade_NAVServerInstance `

-NAVServerServiceAccount $NAVUpgrade_NAVServerServiceAccount `

-DatabaseServer $NAVUpgrade_DatabaseServer `

-DatabaseInstance $NAVUpgrade_DatabaseInstance `

-DatabaseName $NAVUpgrade_DatabaseName

# Import the new version license into the application database, and restart the server in order for the license to be loaded
        Import-NAVServerLicense -ServerInstance $NAVUpgrade_NAVServerInstance -LicenseFile $NAVUpgrade_CurrentVersionLicenseFile -Database NavDatabase

Set-NAVServerInstance -ServerInstance $NAVUpgrade_NAVServerInstance -Restart

# Synchronize the NAV database
        Sync-NAVTenant -ServerInstance $NAVUpgrade_NAVServerInstance -Mode Sync -Force

 

#5. Imports the application objects and upgrade toolkit objects from the specified .fob file, and then synchronizes the table schema again.

#   This updates the SQL Server database based on the new table schema that is defined by the imported application objects. Data that must be mapped to another table is saved in upgrade tables.

# Delete the tables from the previous version, using SynchronizeSchemaChanges Later.

# The new  objects we import will contain the new version of the tables.
        Delete-NAVApplicationObject `

-DatabaseName $NAVUpgrade_DatabaseName `

-DatabaseServer $NAVUpgrade_DatabaseSQLServerInstance `

-LogPath $NAVUpgrade_UpgradeLogsDirectory `

-Filter "Type=Table;ID=<2000000000" `

-SynchronizeSchemaChanges "No" `

-NavServerName $NavServerInfo.NavServerName `

-NavServerInstance $NAVServerInfo.NavServerInstance `

-NavServerManagementPort $NavServerInfo.NavServerManagementPort `

-Confirm:$false

# Import all the new objects and the upgrade objects, by delaying the schema synchronization

# If an $UpgradeToolkitObjects value has not been provided, then

#  the assumption is that the upgrade toolkit is within the same .FOB as the new objects
           if(!$UpgradeToolkitObjects)

{

# Import FOB file containing the new version of the application objects, including the upgrade toolkit

Import-NAVApplicationObject `

-Path $NAVUpgrade_NewVersionObjectsFobFilePath `

-DatabaseName $NAVUpgrade_DatabaseName `

-DatabaseServer $NAVUpgrade_DatabaseSQLServerInstance `

-LogPath $NAVUpgrade_UpgradeLogsDirectory `

-ImportAction "Overwrite" `

-SynchronizeSchemaChanges "No" `

-NavServerName $NavServerInfo.NavServerName `

-NavServerInstance $NAVServerInfo.NavServerInstance `

-NavServerManagementPort $NavServerInfo.NavServerManagementPort `

-Confirm:$false

}

else

{

 

# Import FOB file containing the new version of the application objects

Import-NAVApplicationObject `

-Path $NAVUpgrade_NewVersionObjectsFobFilePath `

-DatabaseName $NAVUpgrade_DatabaseName `

-DatabaseServer $NAVUpgrade_DatabaseSQLServerInstance `

-LogPath $NAVUpgrade_UpgradeLogsDirectory `

-ImportAction "Overwrite" `

-SynchronizeSchemaChanges "No" `

-Confirm:$false

 

# Import FOB file containing the upgrade codeunit and upgrade tables

Import-NAVApplicationObject `

-Path $NAVUpgrade_UpgradeToolkitObjectsFobFilePath `

-DatabaseName $NAVUpgrade_DatabaseName `

-DatabaseServer $NAVUpgrade_DatabaseSQLServerInstance `

-LogPath $NAVUpgrade_UpgradeLogsDirectory `

-ImportAction "Overwrite" `

-SynchronizeSchemaChanges "No" `

-Confirm:$false

}

# Synchronize the metadata changes to SQL
        Sync-NAVTenant -ServerInstance $NAVUpgrade_NAVServerInstance -Mode Sync -Force

#6. Calls the Start-NAVDataUpgrade cmdlet to verify the data upgrade preconditions and transfer data from the upgrade tables to the destination tables.

# Invoke the Data Upgrade process
        Invoke-NAVDataUpgrade -ServerInstance $NAVUpgrade_NAVServerInstance

 

#7. Deletes all obsolete tables and the upgrade toolkit objects.

# Delete Upgrade Toolkit objects
        Delete-NAVApplicationObject `

-DatabaseName $NAVUpgrade_DatabaseName `

-DatabaseServer $NAVUpgrade_DatabaseSQLServerInstance `

-LogPath $NAVUpgrade_UpgradeLogsDirectory `

-Filter "$NAVUpgrade_UpgradeObjectsFilter;ID=<2000000000" `

-SynchronizeSchemaChanges "Force" `

-NavServerName $NavServerInfo.NavServerName `

-NavServerInstance $NAVServerInfo.NavServerInstance `

-NavServerManagementPort $NavServerInfo.NavServerManagementPort `

-Confirm:$false

 

#8.       Initializes all companies in the upgraded database. If you specified a RapidStart package in the Set-PartnerSettings.ps1 file, the package is applied to all companies.

# Optionally, run RapidStart package import
        if($NAVUpgrade_RapidStartPackageFile)

{

Invoke-NAVRapidStartDataImport -ServerInstance      $NAVUpgrade_NAVServerInstance -RapidStartPackageFile $NAVUpgrade_RapidStartPackageFile

 

}

 

The sample script is intended to be run in the context of a Microsoft Dynamics NAV 2016 deployment, including the Microsoft Dynamics NAV Server instance.

The Microsoft Dynamics NAV Server instance cannot be multitenant. When the sample script runs successfully, the result is a Microsoft Dynamics NAV 2016 database that is connected to a Microsoft Dynamics NAV 2016 Server instance, and which uses a Microsoft Dynamics NAV 2016 license.

You may face some permission related issues, take help of you IT person if not sure about the nature of issue or use Administrator login.

I will come with more details in my next posts.

 

 

3 comments: