NAV 2013R2 Running Powershell Script from C/AL Code

matsn
Member Posts: 10
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
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
0
Comments
-
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).0
-
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.0 -
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!!0 -
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.com0 -
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/dhanrajbansal0 -
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.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