Note that there are some explanatory texts on larger screens.

plurals
  1. POInvoke PowerShell script from c# in runspace and get line number where error occured
    text
    copied!<p>I have some PowerShell hosts in C# from where I execute PowerShell script code. The code below is from a host in an Add0In for Visual Studio. Problem is that if an error occurs in the PowerShell script code that I don't know the file and line number of the PowerShell script file where the error occured.</p> <p>My hosting code looks like:</p> <pre><code> public Exception Execute(string scriptcode, Hashtable variables) { Runspace runspace = null; Pipeline pipeline = null; PipelineStateInfo info = null; try { // Make our output window active Logger.ShowOutputWindow(); // Create the runspace and stuff. runspace = RunspaceFactory.CreateRunspace(host); pipeline = runspace.CreatePipeline(); runspace.Open(); // Add our scriptcode to the pipeline pipeline.Commands.Add(new Command(scriptcode, true)); // We don't want to get PSObjects out of the pipeline, output result as string in default way pipeline.Commands.Add(new Command("Out-Default", true)); // Set up global variables FillVariables(runspace, variables); SetUpOutputStreams(pipeline); // Run the scriptcode Collection&lt;PSObject&gt; psOutput = pipeline.Invoke(); // Did it complete ok? info = pipeline.PipelineStateInfo; if (info.State != PipelineState.Completed) { return info.Reason; } else { return null; // succesful! } } catch (Exception ex) { return ex; } } </code></pre> <p>First I had my script in the scriptcode variable, I now write the code to a temporary .ps1 file first so I could report on linenumbers in that file. But I can't find how to execute code in a file so it is possible to retrieve filename and line numbers in case of errors.</p> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload