NAV 2013R2 Running Powershell Script from C/AL Code

matsnmatsn Member Posts: 10
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 Member Posts: 46
    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 Member Posts: 10
    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 Member Posts: 21
    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 Member Posts: 125
    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 Member Posts: 105
    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 Member, Microsoft Employee Posts: 34
    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.