private void StartProcess(ClientRequestInfo requestInfo)
{
if (requestInfo.Request.Arguments.ContainsHelpRequest || requestInfo.Request.Arguments.OrderedArgCount < 1)
{
bool showAdvancedHelp = requestInfo.Request.Arguments.Exists("advanced");
StringBuilder helpMessage = new StringBuilder();
helpMessage.Append("Starts execution of the specified service or system process.");
helpMessage.AppendLine();
helpMessage.AppendLine();
helpMessage.Append(" Usage:");
helpMessage.AppendLine();
helpMessage.Append(" Start \"Process Name\" -options");
helpMessage.AppendLine();
helpMessage.AppendLine();
helpMessage.Append(" Options:");
helpMessage.AppendLine();
helpMessage.Append(" -?".PadRight(20));
helpMessage.Append("Displays this help message");
helpMessage.AppendLine();
helpMessage.Append(" -args".PadRight(20));
helpMessage.Append("Arguments to be passed in to the process");
helpMessage.AppendLine();
helpMessage.Append(" -restart".PadRight(20));
helpMessage.Append("Aborts the process if executing and start it again");
helpMessage.AppendLine();
helpMessage.Append(" -list".PadRight(20));
helpMessage.Append("Displays list of all service or system processes");
if (m_supportSystemCommands && showAdvancedHelp)
{
helpMessage.AppendLine();
helpMessage.Append(" -system".PadRight(20));
helpMessage.Append("Treats the specified process as a system process");
}
helpMessage.AppendLine();
helpMessage.AppendLine();
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, helpMessage.ToString());
}
else
{
string processName = requestInfo.Request.Arguments["orderedarg1"];
string processArgs = requestInfo.Request.Arguments["args"];
bool systemProcess = requestInfo.Request.Arguments.Exists("system");
bool restartProcess = requestInfo.Request.Arguments.Exists("restart");
bool listProcesses = requestInfo.Request.Arguments.Exists("list");
if (restartProcess)
{
requestInfo.Request = ClientRequest.Parse($"Abort \"{processName}\" {(systemProcess ? "-system" : "")}");
AbortProcess(requestInfo);
}
if (systemProcess && m_supportSystemCommands)
{
// Start system process.
try
{
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Attempting to start system process \"{0}\"...\r\n\r\n", processName);
Process startedProcess = Process.Start(processName, processArgs);
if ((object)startedProcess != null)
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Successfully started system process \"{0}\".\r\n\r\n", processName);
else
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Alarm, "Failed to start system process \"{0}\".\r\n\r\n", processName);
}
catch (Exception ex)
{
LogException(ex);
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Alarm, "Failed to start system process \"{0}\". {1}.\r\n\r\n", processName, ex.Message);
}
}
else
{
// Start service process.
ServiceProcess processToStart = FindProcess(processName);
if ((object)processToStart != null)
{
if (processToStart.CurrentState != ServiceProcessState.Processing)
{
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Attempting to start service process \"{0}\"...\r\n\r\n", processName);
if (string.IsNullOrEmpty(processArgs))
{
processToStart.Start();
}
else
{
// Prepare the arguments.
string[] splitArgs = processArgs.Split(',');
for (int i = 0; i < splitArgs.Length; i++)
{
splitArgs[i] = splitArgs[i].Trim();
}
// Start the service process.
processToStart.Start(splitArgs.Cast<object>().ToArray());
}
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Successfully started service process \"{0}\".\r\n\r\n", processName);
}
else
{
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Alarm, "Failed to start process \"{0}\". Process is already executing.\r\n\r\n", processName);
}
}
else
{
UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Alarm, "Failed to start service process \"{0}\". Process is not defined.\r\n\r\n", processName);
}
}
if (!listProcesses)
return;
requestInfo.Request = ClientRequest.Parse($"Processes {(systemProcess ? "-system" : "")}");
ShowProcesses(requestInfo);
}
}