public override void Execute() { if (ScriptName == null && ScriptCommand == eScriptAct.Script) { Reporter.ToLog(eLogLevel.ERROR, "Script file not Selected. Kindly select suitable file"); this.Error = "Script file not loaded. Kindly select suitable file"; return; } DataBuffer = ""; ErrorBuffer = ""; string TempFileName = ""; System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; //TODO: user customerd script location switch (ScriptInterpreterType) { case eScriptInterpreterType.BAT: break; case eScriptInterpreterType.JS: case eScriptInterpreterType.VBS: if (File.Exists(GetSystemDirectory() + @"\cscript.exe")) { p.StartInfo.FileName = GetSystemDirectory() + @"\cscript.exe"; } else { p.StartInfo.FileName = @"cscript"; } break; case eScriptInterpreterType.SH: p.StartInfo.FileName = "/bin/bash"; break; case eScriptInterpreterType.Other: if (!string.IsNullOrEmpty(ScriptInterpreter)) { p.StartInfo.FileName = WorkSpace.Instance.Solution.SolutionOperations.ConvertSolutionRelativePath(ScriptInterpreter); } break; } p.OutputDataReceived += (proc, outLine) => { AddData(outLine.Data); }; p.ErrorDataReceived += (proc, outLine) => { AddError(outLine.Data); }; p.Exited += Process_Exited; if (string.IsNullOrEmpty(SolutionFolder)) { if (string.IsNullOrEmpty(ScriptPath)) { p.StartInfo.WorkingDirectory = Path.GetDirectoryName(ScriptName); } else { p.StartInfo.WorkingDirectory = ScriptPath; } } else { if (Directory.Exists(Path.Combine(SolutionFolder, "Documents", "scripts"))) { p.StartInfo.WorkingDirectory = Path.Combine(SolutionFolder, "Documents", "scripts"); } else { p.StartInfo.WorkingDirectory = Path.Combine(SolutionFolder, "Documents", "Scripts"); } } p.StartInfo.WorkingDirectory = WorkSpace.Instance.Solution.SolutionOperations.ConvertSolutionRelativePath(p.StartInfo.WorkingDirectory); try { string Params = GetCommandText(this); if (ScriptCommand == eScriptAct.Script) { if (ScriptInterpreterType == eScriptInterpreterType.BAT) { p.StartInfo.FileName = System.IO.Path.Combine(p.StartInfo.WorkingDirectory, ScriptName); p.StartInfo.Arguments = Params; } else if (ScriptInterpreterType == eScriptInterpreterType.SH) { string filePath = Path.Combine(p.StartInfo.WorkingDirectory, ScriptName); p.StartInfo.Arguments = filePath + Params; } else if (ScriptInterpreter != null && ScriptInterpreter.Contains("cmd.exe")) { p.StartInfo.Arguments = " /k " + ScriptName + " " + Params; } else { p.StartInfo.Arguments = "\"" + ScriptName + "\" " + Params; } } else { if (ScriptInterpreterType == eScriptInterpreterType.VBS) { TempFileName = CreateTempFile("vbs"); } else if (ScriptInterpreterType == eScriptInterpreterType.BAT) { TempFileName = CreateTempFile("bat"); p.StartInfo.FileName = TempFileName; } else if (ScriptInterpreterType == eScriptInterpreterType.Other) { if (ScriptInterpreter != null && ScriptInterpreter.ToLower().Contains("cmd.exe")) { TempFileName = CreateTempFile("cmd"); } else if (ScriptInterpreter != null && ScriptInterpreter.ToLower().Contains("powershell.exe")) { TempFileName = CreateTempFile("ps1"); p.StartInfo.Arguments = @"-executionpolicy bypass -file .\" + TempFileName + " " + Params; } else if (ScriptInterpreter != null && ScriptInterpreter.ToLower().Contains("python.exe")) { TempFileName = CreateTempFile("py"); } else if (ScriptInterpreter != null && ScriptInterpreter.ToLower().Contains("perl.exe")) { TempFileName = CreateTempFile("pl"); } else { //TODO: Need to create temp file based on the selected interpreter this.Error = "This type of script is not supported."; } } if (string.IsNullOrEmpty(p.StartInfo.Arguments) && ScriptInterpreterType != eScriptInterpreterType.BAT) { p.StartInfo.Arguments = "\"" + TempFileName + "\""; } File.WriteAllText(TempFileName, Params); } p.Start(); p.BeginOutputReadLine(); p.BeginErrorReadLine(); p.WaitForExit(); while (!p.HasExited) { Thread.Sleep(100); } } catch (Exception e) { Reporter.ToLog(eLogLevel.ERROR, e.Message); this.Error = "Failed to execute the script. Details: " + e.Message; } finally { DeleteTempFile(TempFileName); } if (!string.IsNullOrEmpty(ErrorBuffer)) { Error += ErrorBuffer; } }