NAV2017 Running PowerShell scripts from C/AL code

lightninglightning 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

Best Answers

  • lightninglightning Member Posts: 15
    Answer ✓
    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 problem :)

Answers

  • AntHillMobAntHillMob Member Posts: 79
    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.
  • Jan_VeenendaalJan_Veenendaal Member Posts: 206
    Take a look at codeunit 1651. This uses the dedicated NAV PowerShell runner object.
    Jan Veenendaal
  • lightninglightning Member Posts: 15
    @Kowa , the add-in sure cleared things up, but i ran into another problem - the powershell script i use exports a text file, but does so on the server, not on the client, is there a workaround?
  • lightninglightning Member Posts: 15
    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)
  • lightninglightning Member Posts: 15
    I did manage to run the Runner on client, but the file still won't export.
  • lightninglightning Member Posts: 15
    Answer ✓
    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 problem :)
  • KowaKowa Member Posts: 918
    edited 2017-09-09
    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).aspx
    Kai Kowalewski
  • vijay_gvijay_g Member Posts: 884
    edited 2018-09-20
    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,

  • vijay_gvijay_g Member Posts: 884
    it got it resolved by adding sleep code.

    REPEAT
    SLEEP(1000);
    UNTIL PowerShellRunner.IsCompleted;
Sign In or Register to comment.