Dynamics NAV PowerShell
What is Power Shell?
Built on the .NET Framework, PowerShell is a task-based command-line shell and scripting language; it is designed specifically for system administrators and power-users, to rapidly automate the administration of multiple operating systems (Linux, macOS, Unix, and Windows) and the processes related to the applications that run on those operating systems.
PowerShell is open source
PowerShell base source code is now available in GitHub and open to community contributions.
Examples,
- Get-Process - Display all process running on the system
- Clear-Host - Clear Display
Microsoft Dynamics NAV Power Shell
Microsoft Dynamics NAV 2016 includes Windows PowerShell cmdlets for administration and for development and file management of application object files and extension packages.
The cmdlets are available in two Windows PowerShell modules:
- Microsoft Dynamics NAV 2016 Administration Shell
- Microsoft Dynamics NAV 2016 Development Shell
Microsoft Dynamics NAV 2016 Administration Shell
The Microsoft Dynamics NAV 2016 Administration Shell includes cmdlets for administering the Microsoft Dynamics NAV deployment, such as adding and configuring Microsoft Dynamics NAV server instances, databases, and users. Also included are cmdlets for administering extension packages. The Microsoft Dynamics NAV 2016 Administration Shell is installed with the Microsoft Dynamics NAV Server.
Dynamics NAV Powershell |
Microsoft Dynamics NAV 2016 Development Shell
The Microsoft Dynamics NAV 2016 Development Shell includes cmdlets for merging and modifying application object files. Also included are cmdlets for creating extension packages. The Microsoft Dynamics NAV 2016 Development Shell is installed with the Microsoft Dynamics NAV Development Environment.
Following are the Dynamics NAV modules paths needs to include while execution on Powershell tools
- Microsoft.Dynamics.NAV.Management
- Microsoft.Dynamics.NAV.Apps.Management
- Microsoft.Dynamics.NAV.Apps.Tools
- Microsoft.Dynamics.NAV.Model.Tools
Powershell Tools
By the help of Powersheell tools we are able to create / modify the powershell scripts. This can be done with various tools existing like Windows PowerShell ISE, NAV Administartor, NAV Developmentshell, Visual Studio Code etc.
The cmdlets are implemented in Windows PowerShell 3.0, which is included with Windows Server 2012 and Windows 8 and later.
Windows PowerShell ISE
- Integrated Scripting environment
- Multiline Editing
- Selective Execution
- Context-Sensitive Help
- Debug
- Commands Explorer
- Snippets
- Don’t accept shortcut parameters
VS Code
Powersheel scripts are stored in ps1 extension.
You can write Your own modules or use modules that already online
Powershell Scripts
To see the cmdlets that are available for Microsoft Dynamics NAV
- Get-Command *NAV*
To get Help about syntax and options for a specific cmdlet
- Get-Help
Example, Get-Help Get-NAVServerInstance
To get online Help in the MSDN Library for a specific
Cmdlet
- Get-Help
-online
- The sample Windows PowerShell scripts are available in WindowsPowerShellScripts folder on the Microsoft Dynamics NAV product media.
- Power Shell Can be found in the NAV Service Folder with “NavAdminTool.ps1”
- Copy as Path copying from folder
- Press- Shift + Right Click and then select “Copy as Path”
Windows copy as path option |
How to use Windows PowerShell ISE ?
Open Windows PowerShell ISE
To open the Windows PowerShell ISE , you have to type ISE or Windows PowerShell ISE search
Once you do then will get following screen (left)
Windows PowerShell ISE |
Make sure that you have opening the Windows Power Shell in "Run as Administrator" option to get better result.
Once you open the Windows power shell need make sure right side like windows selected on Windows Power Shell on View menu (This is to make sure all the necessary windows are enabled for view and better understanding only)
Once you complete this first step is over.
2) Import the Module to activate Dynamics NAV commands
Now our step is to Import the NAV cmdlet command via Windows Power Shell
Windows Power Shell ISE screen after NAV Module Import |
Set-ExecutionPolicy unrestricted -Force
Import-Module 'C:\Program Files\Microsoft Dynamics NAV\100\Service\NavAdminTool.ps1'
For entering the command you have paste the given the command on white space available on the above screen.
Once you enter the command just press F5 to execute or as mentioned in the image click on to the green symbol with right arrow.
Note above Query is for the Dynamics NAV 2017 version. If you want to perform the same for other versions the please copy corresponding version queries.
Dynamics NAV 2013
Set-ExecutionPolicy unrestricted -Force
Import-Module 'C:\Program Files\Microsoft Dynamics NAV\70\Service\NavAdminTool.ps1'
Dynamics NAV 2013 R2
Set-ExecutionPolicy unrestricted -Force
Import-Module 'C:\Program Files\Microsoft Dynamics NAV\71\Service\NavAdminTool.ps1'
Dynamics NAV 2015
Set-ExecutionPolicy unrestricted -Force
Import-Module 'C:\Program Files\Microsoft Dynamics NAV\80\Service\NavAdminTool.ps1'
Dynamics NAV 2016
Set-ExecutionPolicy unrestricted -Force
Import-Module 'C:\Program Files\Microsoft Dynamics NAV\90\Service\NavAdminTool.ps1'
Dynamics NAV 2017
Set-ExecutionPolicy unrestricted -Force
Import-Module 'C:\Program Files\Microsoft Dynamics NAV\100\Service\NavAdminTool.ps1'
Dynamics NAV 2018
Set-ExecutionPolicy unrestricted -Force
Import-Module 'C:\Program Files\Microsoft Dynamics NAV\110\Service\NavAdminTool.ps1'
Note: given path may different depending upon the NAV installation location
Once you execute the command system will automatically import all the necessary commands details for corresponding NAV version. So its important to import the same version module to achieve the desired the results.
3) Refresh Commands
Refresh commands is the 3rd step. For doing this you have to press Refresh on right side Commands column available on the Windows Power Shell screen.
Once you refreshed system will populate the Dynamics NAV commands for the imported version.
FYI specifying the important commands available with the Windows Power Shell option
- Get-NavServerUser - Returns information about all users created in the current Microsoft Dynamics NAV database.
- Get-NAVServerUserPermissionSet - Returns permission set information for Microsoft Dynamics NAV users.
- New-NAVServerUser - Creates a new Microsoft Dynamics NAV user.
- New-NAVServerUserPermissionSet - Assigns a permission set to a Microsoft Dynamics NAV user.
- Remove-NAVServerUser - Deletes a Microsoft Dynamics NAV user.
- Remove-NAVServerUserPermissionSet - Removes a permission set from the list of permission sets that are assigned to a Microsoft Dynamics NAV user.
- Set-NAVServerUser - Changes the configuration settings of an existing Microsoft Dynamics NAV user.
Dynamics NAV User ID Creation and Permission Assign via Power shell
Yes. You can able to create the NAV user id as well as assign permission without log in to the SQL and Dynamics NAV Application.All you have to do is that have to import the module as described above and enter the power shell command like below.
PS
C:\Windows\system32> New-NAVServerUser -ServerInstance navdemo -WindowsAccount navisionplanet\jubel -Confirm -LicenseType Full -State Enabled
PS
C:\Windows\system32> New-NAVServerUserPermissionSet -PermissionSetId SUPER
-ServerInstance navdemo -WindowsAccount navisionplanet\jubel -CompanyName "Navision Planet" -Force
Replace the following keywords exists on the above script
- navdemo : Replace this will NAV Administration module defined NAV Instance Name
- navisionplanet\jubel : Replace this with your "Domain Name\ User ID"
- SUPER :- Replace this field with Permission ID you would like to add the user.
- Navision Planet : Replace this value with NAV Company name would like to add to the user.
SQL Database Restoration via Powershell
Dynamics NAV Powershell Script to Restore one SQL Database is as follows
New-NAVDatabase -DatabaseName DBName -FilePath "SQL db back path to be mentioned here" -DatabaseServer SERVER-INSTANCE -DataFilesDestinationPath "DB Storage Path" -LogFilesDestinationPath "DB Log Path"
Copy the above power shell script and enter on Powershell editor and replace following parameters
DBName: Name of the SQL DB required
SQL db back path to be mentioned here: Specify the fbk file full path
DB Storage Path : Replace this with your new SQL DB path
DB Log Path: Replace this with your new SQL DB Log file path
Sample Powershell restore path is as follows
New-NAVDatabase -DatabaseName testDB -FilePath "D:\NAV Sotware\NAV 2016 W1\CU 24 NAV 2016 W1\NAV.9.0.49326.W1.DVD\SQLDemoDatabase\CommonAppData\Microsoft\Microsoft Dynamics NAV\90\Database\Demo Database NAV (9-0).bak" -DatabaseServer 3ALT15B0053\NAVDEMO -DataFilesDestinationPath "D:\NAV Sotware\test" -LogFilesDestinationPath "D:\NAV Sotware\test\Log"
SQL DB Restore Powershell |