I have developed a ASP.NET C# web page which connects to remote conmputer command prompt using PSSERVICES.exe and executes some commands against servers. The execution works fine until I deployed the application on IIS 7.5. The application is supposed to run the commandsn get the output and display to the user. After deployement, the command execution has a strange behaviour since the command never finishes, it seems that the application enters in an infinite loop and the result of the command never returns.
string output = ""; string error=""; string filePath = @"C:\" + area.Name + "" + area.Server.Name + "" + action + ".txt"; UpdateXML_History update_History = new UpdateXML_History();
Process p;
p = new System.Diagnostics.Process();
//Do not receive an event when the process exits.
p.EnableRaisingEvents = false;
/* Init Structure */
CommandsLog _commandlog = new CommandsLog();
_commandlog.StartDateTime = DateTime.Now;
_commandlog.Command = action + " service " + service;
_commandlog.Status = CommandStatus.NOTSTARTED;
_commandlog.Output = "";
/*****************/
//The "/C" Tells Windows to Run The Command then Terminate
ProcessStartInfo PSI = new ProcessStartInfo();
PSI.FileName = @"psService.exe";
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.UseShellExecute = false;
PSI.CreateNoWindow = true;
PSI.WindowStyle = ProcessWindowStyle.Hidden;
PSI.Arguments = @"\\" + area.Server.Name + " -u " + DomainUser.userDomain + "\\" + DomainUser.userName + " -p " + DomainUser.password + " " + action + @" """ + service + " " + area.Name + @"""";
p.StartInfo = PSI;
p.Start();
_commandlog.Status = CommandStatus.RUNNING;
output = p.StandardOutput.ReadToEnd().ToString();
error = p.StandardError.ReadToEnd().ToString();
p.WaitForExit();
if (p.HasExited)
{
}
p.Close();
_commandlog.Status = CommandStatus.COMPLETE;
_commandlog.Output = output + " " + error;
_commandlog.EndDateTime = DateTime.Now;
// Update xml file
update_History.UpdateXML(area, _commandlog);
return output + " " + error;
In debug mode, the execution is stuck into this method. When it reaches p.WaitForExit(); it goes back to the code and executes the command infinitly.
This method is called from a webApi controller called CommandController :
[AcceptVerbs("GET", "POST")]
public Task<string> runCommand(CommandParamsViewModel commandParams)
{
return Task.Run(() =>
{
return ServicePsServicesUtil.LaunchService(...);
});
}
I have switched to Task instead of simple string but they both don't work. I don't know if there is a way to let IIS 7.5 exucute remote commands using PsExec or PsService? Please, I need your help. Thank you !