0

Here is my scenario : I have add-data and add-bulkdata cmdlets, both are written in C# deriving from pscmdlet, add-bulkdata takes a csv file and each line is fed to add-data cmdlet. Add-data cmdlet might throw terminating exceptions, if it does I dont know how to receive it in the add-bulkdata cmdlet, in bulkdata cmdlet I get a commandinvocationexception but it does not have the ErrorRecord that the underlying add-data had set. Also if I query pipeline.errors it gives me no information.

What is the best way to handle such scenario?

My Add-Bulkdata ProcessRecord() function 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();
2
  • Have you tried passing your PSCmdlets SessionState property to RunspaceFactory.CreateRunspace? Commented Dec 5, 2012 at 11:54
  • Just realised that won't work since SessionState != InitialSessionState. I'd wager the issue is that you're creating a new runspace with no connection back to the runspace your cmdlet is actually running in. Commented Dec 5, 2012 at 11:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.