Calling C # command-line cmdlets from another powershell cmdlet

Here is my scenario: I have add-data and add-bulkdata cmdlets, both of them are written in C #, based on pscmdletadd-bulkdata accepts the csv file, and each line is passed to the add-data cmdlet. The Add-data cmdlet can throw final exceptions, if it is not, I don’t know how to get it in the add-bulkdata cmdlet, I get it in the bulkdata cmdlet commandinvocationexception, but it doesn’t ErrorRecordhave the basic add-data installed. Also, if I request pipe.errors, it does not give me any information.

What is the best way to handle such a scenario?

My Add-Bulkdata function ProcessRecord()looks something like this:

InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new[] { @"C:\mybinary.dll" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(cmd); //cmd is add-data cmdlet
pipeline.Commands.Add("out-string"); // I have tried with and without this

Collection<PSObject> results = pipeline.Invoke();
Collection<object> errors = pipeline.Error.ReadToEnd();
0

Source: https://habr.com/ru/post/1606498/


All Articles