Backing up Reports of Dynamics NAV using PowerShell

Before starting this post it is of course recommended to create and have backups of the complete server or SQL server in other means that this. But if you for example want an extra file level based backup that could be version controlled then this could be a solution for you. I wrote the script in a hurry so it is functional but maybe not the most clean. However feel free to improve it and let me know. The script is designed to run on Microsoft Dynamics NAV 2017. And was made to facilitate the need to quickly get backups from reports and maybe in the future be able to copy to a version controlled environment.

In our setup we also used a wrapper script that would run this and compress the results to a ZIP file with a Retention Policy at the moment to save space. Said wrapper script could be placed in the Task Scheduler on Windows so it can run every X days for example. However this script that I am sharing here is based to just do one thing and that is giving you exported .fob files and save them on the directory that you specified using the very powerfull cmdlet Export-NAVApplicationObject.

Keep in mind that the default parameters that are used in the script are from my testing environment. You will probably need to tweak the parameters to make them work for your environment. Things like the name of the database or the reports that you would like to export and the version of Microsoft Dynamics NAV that you are running.

<#
.SYNOPSIS
This is a simple Powershell script to backup an array of reports from Microsoft Dynamics NAV

.DESCRIPTION
The script will export reports as .fob files using the Export-NAVApplicationObject cmdlet. They get exported straight to the by you defined Backup Directory.

.EXAMPLE
Backup-NAVReport.ps1
Backup NAV Reports using the defaults. Mostly likely your environment will differ so you can adjust it using the included parameters

.EXAMPLE
Backup-NAVReport.ps1 -Report 50000..50099 -NAVVersion 100 -NAVDatabase NAV
Backup NAV Reports using some standard environment based customization's. For example which reports you want to export or what version of Microsoft Dynamics NAV you are running. Also an important one which can differ is the name of the Database. 

.LINK
https://docs.microsoft.com/en-us/powershell/module/microsoft.dynamics.nav.ide/export-navapplicationobject?view=dynamicsnav-ps-2018
https://perplexity.nl/
#>

[CmdletBinding()]
param
(
    [int[]]$Report = 50000..50099,
    [string]$NAVVersion = "100",
    [string]$NAVDatabase = "NAV",
    [string]$BackupDirectory = "C:\Users\" + $($env:username) + "\Desktop\",
)

#Import all the needed commands used within PowerShell to interact with Dynamics NAV
Import-Module "C:\Program Files (x86)\Microsoft Dynamics NAV$NAVVersion\RoleTailored Client\Microsoft.Dynamics.Nav.Model.Tools.psd1"

#Set the Dynamics NAV Dev Environment which will be used
. "C:\Program Files (x86)\Microsoft Dynamics NAV$NAVVersion\RoleTailored Client\NavModelTools.ps1" -NavIde "C:\Program Files (x86)\Microsoft Dynamics NAV$NAVVersion\RoleTailored Client\finsql.exe" 

if (-Not (Test-Path $BackupDirectory))
{
    New-Item $BackupDirectory -ItemType Directory
}

#For each specified report from the array export it to the Backup Directory as a .fob
foreach ($R in $Report)
{
    Export-NAVApplicationObject ($BackupDirectory + "$R.fob") -DatabaseName $NAVDatabase -Filter "Type=Report;Id=$R"
}

Leave a Comment