Example #1
0
        public static void WithPowerShell(IBuildTaskLog log, Action<PowerShell, IPowerShellHostOutput> action)
        {
            Environment.SetEnvironmentVariable("PSExecutionPolicyPreference", "Bypass");

            using (var powerShell = PowerShell.Create())
            using (var powerShellHostOutput = new PowerShellHostOutput(log, new PowerShellCallStackProvider(powerShell)))
            {
                var host = new PowerShellHost(powerShellHostOutput);
                using (var runspace = RunspaceFactory.CreateRunspace(host))
                {
                    Runspace.DefaultRunspace = runspace;
                    powerShell.Runspace = runspace;
                    powerShell.Streams.Error.DataAdded += (sender, args) =>
                    {
                        powerShellHostOutput.WriteError(powerShell.Streams.Error[args.Index]);
                    };

                    runspace.Open();

                    try
                    {
                        action(powerShell, powerShellHostOutput);
                    }
                    catch (RuntimeException e)
                    {
                        if (e.ErrorRecord == null)
                        {
                            throw;
                        }

                        powerShellHostOutput.WriteError(e.ErrorRecord);
                    }
                }
            }
        }
Example #2
0
        internal static void Execute(CommandFactory commandFactory, IBuildTaskLog log)
        {
            PowerShellHost.WithPowerShell(log, (shell, output) =>
            {
                var command = commandFactory.CreateCommand(new PowerShellCommandParameterProvider());

                shell.Commands.AddCommand(command);

                var outputList = new PowerShellOutputList(output, new PowerShellStringProvider(shell));
                shell.Invoke(null, outputList);
            });
        }
 public PowerShellHostOutput(IBuildTaskLog log, IPowerShellCallStackProvider callStackProvider)
 {
     _log = log;
     _callStackProvider = callStackProvider;
 }