NAV2017 Running PowerShell scripts from C/AL code

lightning
Member Posts: 15
Hi,
I would like to export NAV objects using code instead of the Dev Environment tool and i have the cmdlet for this in a .ps1 file.
Question: Can i run a powershell script from NAV itself (ex. an Action button) without the need for creating a .net extension library? Any tip appreciated
I would like to export NAV objects using code instead of the Dev Environment tool and i have the cmdlet for this in a .ps1 file.
Question: Can i run a powershell script from NAV itself (ex. an Action button) without the need for creating a .net extension library? Any tip appreciated
2
Best Answers
-
No problem, from NAV 2016 onwards there is an add-in
https://blogs.msdn.microsoft.com/nav/2015/12/21/coffee-break-use-the-powershell-runner-add-in/
But you can do it with any NAV Version, even 2.x. For NAV 2009 I provided some sample objects on how to do this here:
http://www.msdynamics.de/viewtopic.php?f=17&t=28159Kai Kowalewski5 -
Managed to make it work. Saves a file to client and does it fast.
Here's the code:PowerShellRunner := PowerShellRunner.CreateInSandbox; PowerShellRunner.WriteEventOnError := TRUE; PowerShellRunner.ImportModule('C:\Commandlets\cmdletExport.ps1'); PowerShellRunner.AddCommand('Export-NAVApplicationObjectFile'); PowerShellRunner.AddParameter('Database','NAV2016PL'); PowerShellRunner.AddParameter('WorkingFolder','D:\'); PowerShellRunner.AddParameter('ExportFile','CU1.txt'); PowerShellRunner.AddParameter('Filter','Type=Codeunit;ID=1'); PowerShellRunner.AddParameter('Verbose'); PowerShellRunner.BeginInvoke; REPEAT SLEEP(1000); UNTIL PowerShellRunner.IsCompleted;
The commandlet i used instead of the one NAV provides is this one:
http://www.waldo.be/2014/06/04/nav-2013-r2-export-objects-with-powershell-3/
The PowerShellRunner works on client (DotNet variable), and the sleep after the Invoke makes sure nav doesn't close the PowerShell before it finishes the entire script
I appreciate every tip, you were all a great help and i hope this will help everyone with a similar problem1
Answers
-
I have never tried this but in theory Powershell is part of the System.Management.Automation namespace so you could just declare a .net variable and try and access it this way.
https://msdn.microsoft.com/en-us/library/system.management.automation.powershell(v=vs.85).aspx
Let the forum know how you get on.
I should add that the use of .net in NAV will be limited in the future therefore you may go down a route that is not futureproof with this approach.0 -
Take a look at codeunit 1651. This uses the dedicated NAV PowerShell runner object.Jan Veenendaal0
-
No problem, from NAV 2016 onwards there is an add-in
https://blogs.msdn.microsoft.com/nav/2015/12/21/coffee-break-use-the-powershell-runner-add-in/
But you can do it with any NAV Version, even 2.x. For NAV 2009 I provided some sample objects on how to do this here:
http://www.msdynamics.de/viewtopic.php?f=17&t=28159Kai Kowalewski5 -
Actually it seems it does not work at all, perhaps i cannot use the add-in correctly:
My code is as follows:PowerShellRunner := PowerShellRunner.CreateInSandbox; PowerShellRunner.WriteEventOnError := TRUE; PowerShellRunner.ImportModule('C:\Program Files (x86)\Microsoft Dynamics NAV\90\RoleTailored Client\Microsoft.Dynamics.Nav.Model.Tools.psd1'); PowerShellRunner.AddCommand('Export-NAVApplicationObject'); PowerShellRunner.AddParameter('DatabaseName','NSC3'); PowerShellRunner.AddParameter('Path','c:\temp\testexportNAV.txt'); PowerShellRunner.AddParameter('DatabaseServer',''); PowerShellRunner.AddParameter('Filter','Type=Codeunit;Id=1'); PowerShellRunner.AddParameter('Verbose'); PowerShellRunner.AddParameter('Force'); PowerShellRunner.WriteEventOnError := TRUE; PowerShellRunner.BeginInvoke;
I even added a long sleep period so the runner won't close
The DotNet variable is RunOnClient: No, since when i try to set that to true, NAV returns an error saying that it cannot find the file and it's dependencies (the PowerShellRunner file)0 -
I did manage to run the Runner on client, but the file still won't export.0
-
Managed to make it work. Saves a file to client and does it fast.
Here's the code:PowerShellRunner := PowerShellRunner.CreateInSandbox; PowerShellRunner.WriteEventOnError := TRUE; PowerShellRunner.ImportModule('C:\Commandlets\cmdletExport.ps1'); PowerShellRunner.AddCommand('Export-NAVApplicationObjectFile'); PowerShellRunner.AddParameter('Database','NAV2016PL'); PowerShellRunner.AddParameter('WorkingFolder','D:\'); PowerShellRunner.AddParameter('ExportFile','CU1.txt'); PowerShellRunner.AddParameter('Filter','Type=Codeunit;ID=1'); PowerShellRunner.AddParameter('Verbose'); PowerShellRunner.BeginInvoke; REPEAT SLEEP(1000); UNTIL PowerShellRunner.IsCompleted;
The commandlet i used instead of the one NAV provides is this one:
http://www.waldo.be/2014/06/04/nav-2013-r2-export-objects-with-powershell-3/
The PowerShellRunner works on client (DotNet variable), and the sleep after the Invoke makes sure nav doesn't close the PowerShell before it finishes the entire script
I appreciate every tip, you were all a great help and i hope this will help everyone with a similar problem1 -
Files are always created on the server in a 3-tier environment, you have to transfer them to the client as explained here:
https://msdn.microsoft.com/en-us/library/dd338733(v=nav.90).aspxKai Kowalewski0 -
Hi,
i am trying with same code on my local system where service is installed but could not export object file with no error. can't figure out what i am doing wrong.
i am checking event viewer also but no error message there. i am using same code as above.
can you please help out? Thanks,
0 -
it got it resolved by adding sleep code.
REPEAT
SLEEP(1000);
UNTIL PowerShellRunner.IsCompleted;0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions