internal void Execute(ApplicationJob application, ApplicationJobError errorInfo = null) { using (PowerShell powerShell = PowerShell.Create()) { powerShell.AddScript(this.scriptText); // Make application object available to the script. if (application != null) { powerShell.Runspace.SessionStateProxy.SetVariable("app", application); } if (errorInfo != null) { powerShell.Runspace.SessionStateProxy.SetVariable("apperror", errorInfo); } powerShell.Runspace.SessionStateProxy.SetVariable("globalvars", UrlVariable.GlobalVariables); // Output all information we can get. powerShell.Streams.Debug.DataAdded += this.DebugDataAdded; powerShell.Streams.Warning.DataAdded += this.WarningDataAdded; try { powerShell.Streams.Information.DataAdded += this.InfoDataAdded; } catch (MissingMethodException) { // Only supported in PS 5.0 and higher } Collection <PSObject> psOutput = powerShell.Invoke(); foreach (PSObject outputItem in psOutput) { // if null object was dumped to the pipeline during the script then a null // object may be present here. check for null to prevent potential NRE. if (outputItem != null) { this.LastOutput = outputItem.ToString(); LogDialog.Log("PowerShell: " + outputItem); } } if (powerShell.HadErrors) { StringBuilder sb = new StringBuilder(); foreach (ErrorRecord error in powerShell.Streams.Error) { sb.AppendLine(error.Exception.Message); } throw new ApplicationException(sb.ToString()); } } }
/// <summary> /// Handles download failure (set failed state, add to errors) and executes the "update failed" /// command for additional control. /// </summary> private void HandleUpdateFailed(ApplicationJob job, ApplicationJobError error) { // Execute: Default update failed command string updateFailedCommand = Settings.GetValue("UpdateFailedCommand", "") as string; ScriptType defaultPreCommandType = Command.ConvertToScriptType(Settings.GetValue("UpdateFailedCommandType", ScriptType.Batch.ToString()) as string); m_Status[job] = Status.Failure; if (!string.IsNullOrEmpty(updateFailedCommand)) { int exitCode = new Command(updateFailedCommand, defaultPreCommandType).Execute(job, null, error); // Do not show failure in error window. if (exitCode == 1) { LogDialog.Log(job, "Update failed command returned '1', ignoring error"); return; } } m_Errors.Add(error); }
/// <summary> /// Executes the command. /// </summary> /// <param name="targetFileName">Content for variable "{url:...}"</param> public virtual int Execute(ApplicationJob application, string targetFileName = null, ApplicationJobError errorInfo = null) { switch (Type) { case ScriptType.CS: UserCSScript script = new UserCSScript(this.Text); script.Execute(application); break; case ScriptType.PowerShell: PowerShellScript psScript = new PowerShellScript(this.Text); psScript.Execute(application, errorInfo); return(Conversion.ToInt(psScript.LastOutput)); default: return(ExecuteBatchCommand(application, this.Text, targetFileName)); } return(0); }