public DeployAgentResult Execute(string commandText, IDictionary <string, object> variables)
        {
            var    hasErrors = true;
            string output;

            var ui = new DeploymentHostUI();

            try
            {
                var host = new DeploymentHost(ui);
                using (var space = RunspaceFactory.CreateRunspace(host))
                {
                    space.Open();

                    if (null != variables)
                    {
                        foreach (var key in variables.Keys)
                        {
                            space.SessionStateProxy.SetVariable(key, variables[key]);
                        }
                    }

                    using (var pipeline = space.CreatePipeline())
                    {
                        var scriptCommand = new Command(commandText, true);
                        scriptCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
                        pipeline.Commands.Add(scriptCommand);

                        pipeline.Commands.Add("Out-Default");

                        pipeline.Commands.AddScript(@"Get-Job | Remove-Job -Force");
                        pipeline.Invoke();

                        hasErrors = ui.HasErrors;
                        output    = ui.Output;
                    }
                }
            }
            catch (RuntimeException ex)
            {
                var record = ex.ErrorRecord;
                var sb     = new StringBuilder();
                sb.AppendLine(record.Exception.ToString());
                sb.AppendLine(record.InvocationInfo.PositionMessage);
                output = string.Format("{0}\n{1}", ui.Output, sb);
            }
            catch (Exception ex)
            {
                output = string.Format("{0}\n{1}", ui.Output, ex);
            }

            return(new DeployAgentResult {
                HasErrors = hasErrors, Output = output
            });
        }
        public void ExecuteCommand(string command, IDictionary <string, object> variables)
        {
            try
            {
                _errorOccurred = true;

                var ui   = new DeploymentHostUI();
                var host = new DeploymentHost(ui);
                using (var space = RunspaceFactory.CreateRunspace(host))
                {
                    if (ExecutionPolicyBehaviour == PowerShellExecutionPolicyBehaviour.Unrestricted)
                    {
                        InjectUnrestrictedAuthorizationPolicy(space.RunspaceConfiguration);
                    }
                    space.Open();

                    if (null != variables)
                    {
                        foreach (string key in variables.Keys)
                        {
                            space.SessionStateProxy.SetVariable(key, variables[key]);
                        }
                    }

                    using (var pipeline = space.CreatePipeline())
                    {
                        pipeline.StateChanged += PipelineStateChanged;

                        var DefaultOutputCommand = new Command("Out-Default");
                        DefaultOutputCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
                        DefaultOutputCommand.MergeUnclaimedPreviousCommandResults = PipelineResultTypes.Error |
                                                                                    PipelineResultTypes.Output;

                        pipeline.Commands.AddScript(command);
                        pipeline.Commands.Add(DefaultOutputCommand);

                        pipeline.Invoke();
                        _errorOccurred = ui.HasErrors;
                        _output        = ui.Output;
                    }
                }
            }
            catch (RuntimeException ex)
            {
                ErrorRecord record = ex.ErrorRecord;
                var         sb     = new StringBuilder();
                sb.AppendLine(record.Exception.ToString());
                sb.AppendLine(record.InvocationInfo.PositionMessage);
                _output = sb.ToString();
            }
            catch (Exception ex)
            {
                _output = ex.ToString();
            }
        }
        public DeployAgentResult Execute(string commandText, IDictionary <string, object> variables)
        {
            var registerHostScript      = string.Format("[{0}]::{1}($Host.UI)", typeof(DeploymentHostTextWriter).FullName, "RegisterHostUserInterface");
            var setOutputEncodingScript = "$OutputEncoding = [System.Console]::OutputEncoding";

            var    hasErrors = true;
            string output;

            _ui = new DeploymentHostUI();
            try
            {
                var host = new DeploymentHost(_ui);
                using (var space = RunspaceFactory.CreateRunspace(host))
                {
                    space.ThreadOptions = PSThreadOptions.ReuseThread;
                    space.Open();

                    if (null != variables)
                    {
                        foreach (var key in variables.Keys)
                        {
                            space.SessionStateProxy.SetVariable(key, variables[key]);
                        }
                    }

                    using (var pipeline = space.CreatePipeline())
                    {
                        var scriptCommand = new Command(commandText, true);
                        scriptCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
                        pipeline.Commands.AddScript(registerHostScript);
                        pipeline.Commands.AddScript(setOutputEncodingScript);

                        pipeline.Commands.Add(scriptCommand);

                        pipeline.Commands.Add("Out-Default");

                        pipeline.Commands.AddScript(@"Get-Job | Remove-Job -Force");
                        pipeline.Invoke();

                        hasErrors = _ui.HasErrors;
                        output    = _ui.Output;
                    }
                }
            }
            catch (RuntimeException ex)
            {
                var record = ex.ErrorRecord;
                var sb     = new StringBuilder();
                sb.AppendLine(record.Exception.ToString());
                sb.AppendLine(record.InvocationInfo.PositionMessage);
                output = string.Format("{0}\n{1}", _ui.Output, sb);
            }
            catch (Exception ex)
            {
                output = string.Format("{0}\n{1}", _ui.Output, ex);
            }

            return(new DeployAgentResult {
                HasErrors = hasErrors, Output = output
            });
        }