Use function from powershell cmdlet dll

829 views Asked by At

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.

1

There are 1 answers

1
Daniel Göhler On

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();