示例#1
0
        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();
        }
示例#2
0
 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);
     }
 }
示例#3
0
 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);
     }
 }