Example #1
0
        public override async Task ExecuteAsync(IOperationExecutionContext context)
        {
            if (context.Simulation && !this.RunOnSimulation)
            {
                this.LogInformation("Executing PowerShell script...");
                return;
            }

            var jobRunner = context.Agent.GetService <IRemoteJobExecuter>();

            var job = new ExecutePowerShellJob
            {
                ScriptText       = this.ScriptText,
                DebugLogging     = this.DebugLogging,
                VerboseLogging   = this.VerboseLogging,
                CollectOutput    = false,
                LogOutput        = true,
                Variables        = PowerShellScriptRunner.ExtractVariables(this.ScriptText, context),
                Isolated         = this.Isolated,
                WorkingDirectory = context.WorkingDirectory
            };

            job.MessageLogged += (s, e) => this.Log(e.Level, e.Message);

            job.ProgressUpdate += (s, e) => Interlocked.Exchange(ref this.currentProgress, e);

            var result = (ExecutePowerShellJob.Result) await jobRunner.ExecuteJobAsync(job, context.CancellationToken);

            PSUtil.LogExit(this, result.ExitCode, this.SuccessExitCode);
        }
Example #2
0
        public override async Task <PersistedConfiguration> CollectAsync(IOperationCollectionContext context)
        {
            var scriptName = this.DefaultArgument.AsString();

            if (string.IsNullOrWhiteSpace(scriptName))
            {
                this.LogError("Bad or missing script name.");
                return(null);
            }

            if (context.Simulation)
            {
                this.LogInformation("Executing PowerShell Script...");
                return(null);
            }

            var result = await PSUtil.ExecuteScriptAssetAsync(
                logger : this,
                context : context,
                fullScriptName : scriptName,
                arguments : this.NamedArguments,
                outArguments : this.OutArguments,
                collectOutput : true,
                progressUpdateHandler : (s, e) => this.currentProgress = e,
                executionMode : PsExecutionMode.Collect
                );

            this.collectedConfiguration = new PSPersistedConfiguration(result);
            return(this.collectedConfiguration);
        }
        public override Task ExecuteAsync(IOperationExecutionContext context)
        {
            if (context.Simulation)
            {
                this.LogInformation("Executing PowerShell Script...");
                return(Complete);
            }

            var fullScriptName = this.DefaultArgument.AsString();

            if (fullScriptName == null)
            {
                this.LogError("Bad or missing script name.");
                return(Complete);
            }

            return(PSUtil.ExecuteScriptAsync(
                       logger: this,
                       context: context,
                       fullScriptName: fullScriptName,
                       arguments: this.NamedArguments,
                       outArguments: this.OutArguments,
                       collectOutput: false,
                       progressUpdateHandler: (s, e) => Interlocked.Exchange(ref this.currentProgress, e)
                       ));
        }
        public override async Task <PersistedConfiguration> CollectAsync(IOperationCollectionContext context)
        {
            if (!this.ValidateConfiguration())
            {
                return(null);
            }

            ExecutePowerShellJob.Result result;

            if (!string.IsNullOrWhiteSpace(this.CollectScriptAsset))
            {
                result = await PSUtil.ExecuteScriptAsync(
                    logger : this,
                    context : context,
                    fullScriptName : this.CollectScriptAsset,
                    arguments : this.CollectScriptParams ?? new Dictionary <string, RuntimeValue>(),
                    outArguments : new Dictionary <string, RuntimeValue>(),
                    collectOutput : !this.UseExitCode,
                    progressUpdateHandler : (s, e) => Interlocked.Exchange(ref this.currentProgress, e)
                    );
            }
            else
            {
                var jobRunner = context.Agent.GetService <IRemoteJobExecuter>();

                var job = new ExecutePowerShellJob
                {
                    ScriptText     = this.CollectScript,
                    DebugLogging   = this.DebugLogging,
                    VerboseLogging = this.VerboseLogging,
                    CollectOutput  = !this.UseExitCode,
                    LogOutput      = this.UseExitCode,
                    Variables      = PowerShellScriptRunner.ExtractVariables(this.CollectScript, context)
                };

                job.MessageLogged  += (s, e) => this.Log(e.Level, e.Message);
                job.ProgressUpdate += (s, e) => Interlocked.Exchange(ref this.currentProgress, e);

                result = await jobRunner.ExecuteJobAsync(job, context.CancellationToken) as ExecutePowerShellJob.Result;
            }

            if (result.ExitCode != null)
            {
                this.LogDebug("Script exit code: " + result.ExitCode);
            }

            return(new KeyValueConfiguration
            {
                Key = this.ConfigurationKey,
                Value = this.UseExitCode ? result.ExitCode?.ToString() : string.Join(", ", result.Output)
            });
        }
Example #5
0
        public override async Task ConfigureAsync(IOperationExecutionContext context)
        {
            if (!this.ValidateConfiguration())
            {
                return;
            }

            _ = await PSUtil.ExecuteScriptAsync(
                logger : this,
                context : context,
                scriptNameOrContent : AH.CoalesceString(this.ConfigureScriptAsset, this.ConfigureScript),
                scriptIsAsset : !string.IsNullOrWhiteSpace(this.ConfigureScriptAsset),
                arguments : this.ConfigureScriptParams ?? new Dictionary <string, RuntimeValue>(),
                outArguments : new Dictionary <string, RuntimeValue>(),
                collectOutput : false,
                progressUpdateHandler : (s, e) => Interlocked.Exchange(ref this.currentProgress, e)
                );
        }
Example #6
0
        public override async Task <PersistedConfiguration> CollectAsync(IOperationCollectionContext context)
        {
            if (!this.ValidateConfiguration())
            {
                return(null);
            }

            var result = await PSUtil.ExecuteScriptAsync(
                logger : this,
                context : context,
                scriptNameOrContent : AH.CoalesceString(this.CollectScriptAsset, this.CollectScript),
                scriptIsAsset : !string.IsNullOrWhiteSpace(this.CollectScriptAsset),
                arguments : this.CollectScriptParams ?? new Dictionary <string, RuntimeValue>(),
                outArguments : new Dictionary <string, RuntimeValue>(),
                collectOutput : !this.UseExitCode,
                progressUpdateHandler : (s, e) => Interlocked.Exchange(ref this.currentProgress, e)
                );

            return(new KeyValueConfiguration
            {
                Key = this.ConfigurationKey,
                Value = this.UseExitCode ? result.ExitCode?.ToString() : string.Join(", ", result.Output)
            });
        }