0

Is it possible to add DLL that implements PowerShell cmdlet to C# project and call its functions as you normally do with classes? The problem is that cmdlet doesn't have suitable functions to call. It has invoke and other stuff instead.

As far as I understand another way to do it is to use System.Management.Automation Namespace. But I'm afraid that it will cause performance overhead if I run the function like 7000 times in a row.

To be exact I have a comdlets like Remove-NAVApplicationObjectLanguage for MS Dynamics Nav that process files and 7000 files to process. I want to wrap it into the library and call it with my additional processing in a way convenient for me.

Ipsit Gaur
  • 2,872
  • 1
  • 23
  • 38
Mak Sim
  • 2,148
  • 19
  • 30
  • 2
    The performance overhead from invoking an instance of the cmdlet in code 7000 times is not gonna be different from invoking it 7000 times at the command line. To answer your question: no, not directly, the cmdlet interface uses `protected` methods, which is why you only see `Invoke()` exposed when creating an instance of a Cmdlet class. – Mathias R. Jessen Oct 06 '17 at 12:04

1 Answers1

2

Microsoft provided a Blog Article Coffee Break: Use the PowerShell Runner Add-In how to run their Dynamics NAV PowerShell Cmdlets from within Dynamics NAV.

If you like to use that in C#, you could use the very same Microsoft.Dynamics.Nav.PowerShellRunner.dll. It is in the Add-Ins Folder of the Service Tier C:\Program Files\Microsoft Dynamics NAV\100\Service\Add-ins\PowerShellRunner.

C# Example:

PowerShellRunner PowerShellRunner = PowerShellRunner.CreateInSandbox();
PowerShellRunner.WriteEventOnError = true;

PowerShellRunner.ImportModule(@"C:\Program Files(x86)\Microsoft Dynamics NAV\100\RoleTailored Client\Microsoft.Dynamics.Nav.Apps.Tools.dll");

PowerShellRunner.AddCommand("Remove-NAVApplicationObjectLanguage");
string[] sources = new string[] { "TAB9.TXT", "TAB14.TXT" };
PowerShellRunner.AddParameter("Source", sources);
PowerShellRunner.AddParameter("Destination", @".\RESULT");

PowerShellRunner.WriteEventOnError = true;

PowerShellRunner.BeginInvoke();
Daniel Göhler
  • 162
  • 1
  • 12
  • Nice one. Was not aware of this since i'm not using this version of Nav yet. But this is not much different from using `System.Management.Automation`. Most probably it is just a wrapper around it. – Mak Sim Oct 26 '17 at 08:37