ParsedExe ParseExe() { ParsedExe pe = new ParsedExe(); // resolve reserved exe names switch (_appDef.ExeFullPath.ToLower()) { // just cmd shell case "[cmd]": pe.ExeType = EExeType.Executable; pe.Path = GetCmdExePath(); pe.CmdLine = ExpandVars(_appDef.CmdLineArgs); break; case "[cmd.file]": case "[cmd.command]": pe.ExeType = EExeType.Executable; pe.Path = GetCmdExePath(); pe.CmdLine = "/c " + ExpandVars(_appDef.CmdLineArgs); break; // just powershell with any command line case "[powershell]": pe.ExeType = EExeType.Executable; pe.Path = GetPowershellExePath(); pe.CmdLine = ExpandVars(_appDef.CmdLineArgs); break; // powershell script file specified in appDef.CmdLineArgs; case "[powershell.file]": pe.ExeType = EExeType.Executable; pe.Path = GetPowershellExePath(); pe.CmdLine = "-executionpolicy bypass -file " + ExpandVars(_appDef.CmdLineArgs); break; // powershell command specified in CmdLineArgs case "[powershell.command]": pe.ExeType = EExeType.Executable; pe.Path = GetPowershellExePath(); pe.CmdLine = "-executionpolicy bypass -command \"" + ExpandVars(_appDef.CmdLineArgs) + "\""; break; // dirigent.AgentCmd command line case "[dirigent.command]": pe.ExeType = EExeType.DirigentCmd; pe.Path = string.Empty; pe.CmdLine = ExpandVars(_appDef.CmdLineArgs); break; default: pe.ExeType = EExeType.Executable; pe.Path = ExpandVars(_appDef.ExeFullPath); pe.CmdLine = ExpandVars(_appDef.CmdLineArgs); break; } return(pe); }
bool LaunchExe(ParsedExe pe) { // try to adopt an already running process (matching by process image file name, regardless of path) if (_appDef.AdoptIfAlreadyRunning) { if (AdoptAlreadyRunning()) { return(true); } } remainingKillOrdersToForcedHardKill = numKillOrdersToForcedHardKill; // start the process var psi = new ProcessStartInfo(); psi.FileName = BuildAbsolutePath(pe.Path); if (!String.IsNullOrEmpty(pe.CmdLine)) { psi.Arguments = pe.CmdLine; } if (_appDef.StartupDir != null) { var dir = ExpandVars(_appDef.StartupDir); psi.WorkingDirectory = BuildAbsolutePath(dir); } psi.WindowStyle = _appDef.WindowStyle switch { EWindowStyle.Normal => ProcessWindowStyle.Normal, EWindowStyle.Minimized => ProcessWindowStyle.Minimized, EWindowStyle.Maximized => ProcessWindowStyle.Maximized, EWindowStyle.Hidden => ProcessWindowStyle.Hidden, _ => ProcessWindowStyle.Normal }; psi.UseShellExecute = false; // allows us using environment variables // // modify the environment // foreach (var x in _appDef.EnvVarsToSet) { var name = x.Key; var value = ExpandVars(x.Value); psi.EnvironmentVariables[name] = value; } if (!String.IsNullOrEmpty(_appDef.EnvVarPathToAppend)) { var name = "PATH"; var postfix = ExpandVars(_appDef.EnvVarPathToAppend); // if relative path is specified, consider it relative to SharedConfig and make it absolute (per each ';' separated segment) postfix = string.Join(";", postfix.Split(';').Select(p => BuildAbsolutePath(p))); psi.EnvironmentVariables[name] = psi.EnvironmentVariables[name] + ";" + postfix; } if (!String.IsNullOrEmpty(_appDef.EnvVarPathToPrepend)) { var name = "PATH"; var prefix = ExpandVars(_appDef.EnvVarPathToPrepend); // if relative path is specified, consider it relative to SharedConfig and make it absolute (per each ';' separated segment) prefix = string.Join(";", prefix.Split(';').Select(p => BuildAbsolutePath(p))); psi.EnvironmentVariables[name] = prefix + ";" + psi.EnvironmentVariables[name]; } foreach (var x in _extraVars) { var name = x.Key; var value = ExpandVars(x.Value); if (!String.IsNullOrEmpty(value)) { psi.EnvironmentVariables[name] = value; } else { psi.EnvironmentVariables.Remove(name); } } // run the process _proc = null; try { log.DebugFormat("StartProc exe \"{0}\", cmd \"{1}\", dir \"{2}\", windowstyle {3}", psi.FileName, psi.Arguments, psi.WorkingDirectory, psi.WindowStyle); _proc = Process.Start(psi); if (_proc != null) { log.DebugFormat("StartProc SUCCESS pid {0}", _proc.Id); // Note: WindowStyle in CreateProcess somehow does not work on console windows, // probably when the window creation happens later in the process life // Please use MainWindowStyles app watcher to set the style for such cases.. } else { log.DebugFormat("StartProc FAILED (no details)"); throw new AppStartFailureException(_appDef.Id, "no details"); } } catch (Exception ex) { log.DebugFormat("StartProc FAILED except {0}", ex.Message); throw new AppStartFailureException(_appDef.Id, ex.Message, ex); } try { SetPriorityClass(_appDef.PriorityClass); } catch (Exception ex) { log.DebugFormat("SetPriority FAILED except {0}", ex.Message); } return(true); } bool LaunchDirigentCmd(ParsedExe pe) { ctrl.Send(new Net.CLIRequestMessage(pe.CmdLine)); return(true); }