public ExecutePowerShellJob.Result Execute()
            {
                using (var runner = new PowerShellScriptRunner {
                    DebugLogging = this.DebugLogging, VerboseLogging = this.VerboseLogging
                })
                {
                    var outputData = new List <RuntimeValue>();

                    runner.MessageLogged += (s, e) => this.MessageLogged(e.Level, e.Message);
                    if (this.LogOutput)
                    {
                        runner.OutputReceived += (s, e) => this.MessageLogged(MessageLevel.Information, e.Output?.ToString());
                    }

                    var outVariables = this.OutVariables.ToDictionary(v => v, v => new RuntimeValue(string.Empty), StringComparer.OrdinalIgnoreCase);

                    if (this.CollectOutput)
                    {
                        runner.OutputReceived +=
                            (s, e) =>
                        {
                            var output = PSUtil.ToRuntimeValue(e.Output);
                            lock (outputData)
                            {
                                outputData.Add(output);
                            }
                        };
                    }

                    runner.ProgressUpdate += (s, e) => this.ProgressUpdate(e.PercentComplete, e.Activity);

                    int?exitCode = runner.RunAsync(this.ScriptText, this.Variables, this.Parameters, outVariables, default).GetAwaiter().GetResult();

                    return(new ExecutePowerShellJob.Result
                    {
                        ExitCode = exitCode,
                        Output = outputData,
                        OutVariables = outVariables
                    });
                }
            }
Exemplo n.º 2
0
            public async Task <Result> ExecuteAsync(string script, Dictionary <string, RuntimeValue> variables, Dictionary <string, RuntimeValue> parameters, string[] outVariables, CancellationToken cancellationToken)
            {
                using (var runner = new PowerShellScriptRunner {
                    DebugLogging = this.DebugLogging, VerboseLogging = this.VerboseLogging
                })
                {
                    var outputData = new List <RuntimeValue>();

                    runner.MessageLogged += (s, e) => this.MessageLogged?.Invoke(this, e);
                    if (this.LogOutput)
                    {
                        runner.OutputReceived += (s, e) => this.OutputReceived?.Invoke(this, e);
                    }

                    var outVariables2 = outVariables.ToDictionary(v => v, v => new RuntimeValue(string.Empty), StringComparer.OrdinalIgnoreCase);

                    if (this.CollectOutput)
                    {
                        runner.OutputReceived +=
                            (s, e) =>
                        {
                            var output = PSUtil.ToRuntimeValue(e.Output);
                            lock (outputData)
                            {
                                outputData.Add(output);
                            }
                        };
                    }

                    runner.ProgressUpdate += (s, e) => this.ProgressUpdate?.Invoke(this, e);

                    int?exitCode = await runner.RunAsync(script, variables, parameters, outVariables2, cancellationToken);

                    return(new Result
                    {
                        ExitCode = exitCode,
                        Output = outputData,
                        OutVariables = outVariables2
                    });
                }
            }