public string ExecuteCommand(string scriptText) { if (scriptText == null) { throw new ArgumentNullException("scriptText"); } try { this.pipelineExecutor = new PipelineExecutor(this.powerShell.Runspace, scriptText); this.pipelineExecutor.OnDataReady += this.PipelineExecutorOnDataReady; this.pipelineExecutor.OnErrorReady += this.PipelineExecutorOnErrorReady; this.pipelineExecutor.Start(); } catch (InvalidRunspaceStateException sessionStateException) { throw new PowerShellException( string.Format(Resources.PowerShellSession.CannotExecuteCommandBecauseSessionIsClosedMessageTemplate, scriptText), sessionStateException); } while (!this.pipelineExecutor.Pipeline.Output.EndOfPipeline) { Thread.Sleep(500); } return this.pipelineOutput.ToString(); }
private void PipelineExecutorOnErrorReady(PipelineExecutor sender, ICollection<object> data) { foreach (object obj in data.Where(o => o != null)) { string message = "Error : " + obj; this.pipelineOutput.AppendLine(message); this.powerShellHost.UI.WriteLine(message); } }
private void PipelineExecutorOnDataReady(PipelineExecutor sender, ICollection<PSObject> data) { foreach (PSObject obj in data.Where(o => o != null)) { string message = obj.ToString(); this.pipelineOutput.AppendLine(message); this.powerShellHost.UI.WriteLine(message); } }