NAV 2013R2 Running Powershell Script from C/AL Code

matsnmatsn Posts: 10Member
edited 2015-03-02 in NAV Three Tier
Hey guys,

has anyone managed to execute Powershell scripts from NAV C/AL Code?
It's no problem to create a ps1 file and execute it with cmd.exe but I want to be able to access events and the output of the script, which cmd.exe is not offering. I tried some approaches with the DotNet System.Automation.Management.dll but it seems that at some point I always run into the error "Dynamic operations can only be performed in homogenous AppDomain". Has someone experienced this and found a solution?

Regards,
Mats

Comments

  • SiStSiSt Posts: 46Member
    I haven't tried it yet. But the error message suggest that you try to use .NET 4 DLLs. Try to use assemblies for an older .NET version (up to 3.5).
  • matsnmatsn Posts: 10Member
    Tried it with the oldest dll version of System.Management.Automation.dll I could find, which is 1.0.0.0. Still the same error :/

    EDIT:
    I think I found a solution. If you change <NetFx40_LegacySecurityPolicy enabled="true"/> to false it works. Not sure what this parameter is controlling though, is it safe to turn it off?
    Also can't get events to work but at least I get the output now.
  • Ian_BachusIan_Bachus Posts: 21Member
    Hi Mats,

    Did you find sample code somewhere using System.Management.Automation.dll in C/AL code? I'm trying to do the same thing (execute a powershell script from within C/AL) and I'm not having luck. I can execute it from the cmd.exe but I would like to stay away from that.

    Thanks!! :D
  • CaponeCapone Posts: 113Member
    Hi,

    I just want to add the information that I found.

    You change/Remove the <NetFx40_LegacySecurityPolicy enabled="true"/> in the file: C:\Program Files\Microsoft Dynamics NAV\71\Service\Microsoft.Dynamics.Nav.Server.exe.config

    Then restart the Dynamics NAV server.

    Here is a code example to run powershell scripts from Visual Studio. I made .net plugin of this but haven't experimented with it yet. So far I at least get the output.
    using System.Management.Automation;
    using System.Management.Automation.Runspaces; 
    
          public String Run(String Script)
            {
                using (PowerShell PowerShellInstance = PowerShell.Create())
                {
                    PowerShellInstance.AddScript(Script);
                    Collection<PSObject> PSOutput = PowerShellInstance.AddCommand("Out-String").Invoke();
    
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (PSObject obj in PSOutput)
                    {
                        stringBuilder.AppendLine(obj.ToString());
                    }
    
                    return stringBuilder.ToString();
            }
    

    Edit: This won't work for NAV powershell cmdlets since they require admin rights.
    Hello IT, have you tried to turn it off and on?
    Have you checked the cables?
    Have you released the filters?

    http://www.navfreak.com
  • DRBDRB Posts: 100Member
    Hi,
    Is there any security risk when

    NetFx40_LegacySecurityPolicy enabled="true

    is removed or changed to false in Microsoft.Dynamics.Nav.Client.exe.config?
    Is it safe in terms of performance and security etc.? What are the disadvantages associated with this change?
    -Dhan Raj Bansal
    Linkedin Profile: http://in.linkedin.com/in/dhanrajbansal
  • JasminkaTJasminkaT Posts: 34Member, Microsoft Employee
    We have added this property and set it to true to support memory usage and performance of reports. We are also aware about this being a problem with some external add-ins that depend on this policy to be set to false.
    Note though, a possible issue has been reported recently when using this setting with value false: Depending on value of this switch and .net framework version, string comparison might yield different results in specific circumstances.
    To be exact: Reserved Quantity field (table 32, ILE) might show incorrect value (0), due to string interpretation of flowfield condition (Source ID = '', Source Batch Name = ''). This issue is being addressed.
Sign In or Register to comment.