private void TryOutputString(string val)
        {
            if (ReplWindow != null)
            {
                if (val.StartsWith("[ERROR]"))
                {
                    ReplWindow.WriteError(val);
                }
                else
                {
                    ReplWindow.WriteOutput(val);
                }
            }

            if (OutputString != null)
            {
                OutputString(val);
            }
        }
Example #2
0
        /// <summary>
        /// Output string from debugger in VS output/REPL pane window
        /// </summary>
        /// <param name="output"></param>
        public void VsOutputString(string output)
        {
            if (ReplWindow != null)
            {
                if (output.StartsWith(PowerShellConstants.PowerShellOutputErrorTag))
                {
                    ReplWindow.WriteError(output);
                }
                else
                {
                    ReplWindow.WriteOutput(output);
                }
            }

            if (OutputString != null)
            {
                OutputString(output);
            }
        }
        /// <summary>
        /// Execute the specified command line.
        /// </summary>
        /// <param name="commandLine">Command line to execute.</param>
        public void Execute(string commandLine)
        {
            Log.Info("Execute");

            if (_runspace.RunspaceAvailability != RunspaceAvailability.Available)
            {
                throw new InvalidPipelineStateException("Runspace is not available for execution.");
            }

            try
            {
                using (_currentPowerShell = PowerShell.Create())
                {
                    _currentPowerShell.Runspace = _runspace;
                    _currentPowerShell.AddScript(commandLine);

                    _currentPowerShell.AddCommand("out-default");
                    _currentPowerShell.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);

                    var objects = new PSDataCollection <PSObject>();
                    objects.DataAdded += objects_DataAdded;

                    _currentPowerShell.Invoke(null, objects);
                }
            }
            catch (Exception ex)
            {
                Log.Info("Terminating error", ex);
                if (OutputString != null)
                {
                    OutputString(this, new EventArgs <string>("Error: " + ex.Message + Environment.NewLine));
                }

                ReplWindow.WriteError("Error: " + ex.Message + Environment.NewLine);

                OnTerminatingException(ex);
            }
            finally
            {
                DebuggerFinished();
            }
        }