override public ExecuteResult Execute(HandlerStartInfo startInfo) { ExecuteResult result = null; if (startInfo.Parameters != null) { parameters = HandlerUtils.Deserialize <CommandHandlerParameters>(startInfo.Parameters); } try { OnLogMessage("Execute", $"Running Handler As User [{System.Security.Principal.WindowsIdentity.GetCurrent().Name}]"); Validate(); // Replace Any "Special" Handler Variables In Arguments or ReplaceWith elements variables = HandlerUtils.GatherVariables(this, startInfo); parameters.Arguments = HandlerUtils.ReplaceHandlerVariables(parameters.Arguments, variables); if (parameters.Expressions != null) { foreach (RegexArguments expression in parameters.Expressions) { expression.ReplaceWith = HandlerUtils.ReplaceHandlerVariables(expression.ReplaceWith, variables); } } String args = RegexArguments.Parse(parameters.Arguments, parameters.Expressions); bool isDryRun = startInfo.IsDryRun && !(config.SupportsDryRun); if (startInfo.IsDryRun && config.SupportsDryRun) { OnLogMessage("Execute", "DryRun Flag is set, but plan config indicates the command supports DryRun. Command will execute."); } if (String.IsNullOrEmpty(config.RunOn)) { SecurityContext runAs = startInfo.RunAs; if (runAs != null && runAs.HasCrypto) { runAs = startInfo.RunAs.GetCryptoValues(startInfo.RunAs.Crypto, false); } result = LocalProcess.RunCommand(config.Command, args, config.WorkingDirectory, config.TimeoutMills, config.TimeoutStatus, SynapseLogger, null, isDryRun, config.ReturnStdout, runAs?.Domain, runAs?.UserName, runAs?.Password); } else { result = WMIUtil.RunCommand(config.Command, args, config.RunOn, config.WorkingDirectory, config.TimeoutMills, config.TimeoutStatus, config.KillRemoteProcessOnTimeout, SynapseLogger, config.RunOn, isDryRun, config.ReturnStdout); } if (result.Status == StatusType.None) { result.Status = HandlerUtils.GetStatusType(result.ExitCode, config.ValidExitCodes); } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); throw e; } OnLogMessage(config.RunOn, "Command finished with exit code = " + result.ExitCode + ". Returning status [" + result.Status + "]."); return(result); }
override public ExecuteResult Execute(HandlerStartInfo startInfo) { ExecuteResult result = null; if (startInfo.Parameters != null) { parameters = HandlerUtils.Deserialize <ScriptHandlerParameters>(startInfo.Parameters); } String script = null; try { String command = null; String args = null; bool isTempScript = false; OnLogMessage("Execute", $"Running Handler As User [{System.Security.Principal.WindowsIdentity.GetCurrent().Name}]"); Validate(); // Replace Any "Special" Handler Variables In Arguments or ReplaceWith elements variables = HandlerUtils.GatherVariables(this, startInfo); parameters.Arguments = HandlerUtils.ReplaceHandlerVariables(parameters.Arguments, variables); if (parameters.Expressions != null) { foreach (RegexArguments expression in parameters.Expressions) { expression.ReplaceWith = HandlerUtils.ReplaceHandlerVariables(expression.ReplaceWith, variables); } } switch (config.Type) { case ScriptType.Powershell: command = "powershell.exe"; if (!String.IsNullOrWhiteSpace(parameters.Script)) { isTempScript = false; script = parameters.Script; } else { isTempScript = true; script = CreateTempScriptFile(parameters.ScriptBlock, "ps1"); } args = config.Arguments + @" -File """ + script + @""""; if (!String.IsNullOrWhiteSpace(parameters.Arguments)) { String scriptArgs = RegexArguments.Parse(parameters.Arguments, parameters.Expressions); args += " " + scriptArgs; } break; case ScriptType.Batch: command = "cmd.exe"; if (!String.IsNullOrWhiteSpace(parameters.Script)) { isTempScript = false; script = parameters.Script; } else { isTempScript = true; script = CreateTempScriptFile(parameters.ScriptBlock, "bat"); } args = config.Arguments + @" """ + script + @""""; if (!String.IsNullOrWhiteSpace(parameters.Arguments)) { String scriptArgs = RegexArguments.Parse(parameters.Arguments, parameters.Expressions); args += " " + scriptArgs; } break; default: throw new Exception("Unknown ScriptType [" + config.Type.ToString() + "] Received."); } bool isDryRun = startInfo.IsDryRun && !(config.SupportsDryRun); if (startInfo.IsDryRun && config.SupportsDryRun) { OnLogMessage("Execute", "DryRun Flag is set, but plan config indicates the script supports DryRun. Script will execute."); } if (String.IsNullOrEmpty(config.RunOn)) { SecurityContext runAs = startInfo.RunAs; if (runAs != null && runAs.HasCrypto) { runAs = startInfo.RunAs.GetCryptoValues(startInfo.RunAs.Crypto, false); } result = LocalProcess.RunCommand(command, args, config.WorkingDirectory, config.TimeoutMills, config.TimeoutStatus, SynapseLogger, null, isDryRun, config.ReturnStdout, runAs?.Domain, runAs?.UserName, runAs?.Password); } else { result = WMIUtil.RunCommand(command, args, config.RunOn, config.WorkingDirectory, config.TimeoutMills, config.TimeoutStatus, config.KillRemoteProcessOnTimeout, SynapseLogger, config.RunOn, isDryRun, config.ReturnStdout); } if (result.Status == StatusType.None) { result.Status = HandlerUtils.GetStatusType(result.ExitCode, config.ValidExitCodes); } if (File.Exists(script) && isTempScript) { File.Delete(script); } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); if (File.Exists(script)) { File.Delete(script); } throw e; } OnLogMessage(config.RunOn, "Command finished with exit code = " + result.ExitCode + ". Returning status [" + result.Status + "]."); return(result); }