0

When i run the code in debug mode the test completes, when i run it in a run test the ps exec seems to hang and never return i put the kill in place also but it never finished.

why does this complete on debug but not in a run mode.

is there anything silly here which i am missing?

public class RemoteExeInvokerTask :IQaTask
{

  public string TargetHostName { get; set; }
  public string TargetHostUserName { get; set; }
  public string TargetPassword { get; set; }
  public string TargetExecutableWithParams { get; set; }


  public string DoWork()
  {
    string psExecUtility =  Path.GetDirectoryName(Assembly
                                                  .GetExecutingAssembly()
                                                  .Location)
                            + "\\PsTools\\psExec.exe";


    string quotedArgs = string.Format("\\\\{0} -u {1} -p {2} {3} ",
                                      TargetHostName,
                                      TargetHostUserName,
                                      TargetPassword,
                                      TargetExecutableWithParams);


    ProcessStartInfo processInfo = new ProcessStartInfo(psExecUtility, quotedArgs);
    processInfo.RedirectStandardError = true;
    processInfo.RedirectStandardOutput = true;
    processInfo.UseShellExecute = false;
    processInfo.CreateNoWindow = true;
    Process process = Process.Start(processInfo);

    process.WaitForExit();
    string error = process.StandardError.ReadToEnd();
    string output = process.StandardOutput.ReadToEnd();
    int exitCode = process.ExitCode;
    process.Close();

    if (Process.GetProcessById(process.Id) != null)
    {
      process.Kill();
    }




    if (exitCode <= -1)
    {
      string errorMessage = string.Format("Process Exited with ErrorMessge {0} {1} "
                                          + "OutputMessage {2}",
                                          error,
                                          Environment.NewLine,
                                          output);

      throw new MyApplicationException(errorMessage);
    }

      return output;
  }

Test code.

[Test]
[ExpectedException(typeof(MyApplicationException))]
[Description("Expect a exception message form the Server.")]
public void RemoteInvokeOfJobWrapperFromCommandLine()
{
  RemoteExeInvokerTask task = new RemoteExeInvokerTask {
                                TargetHostName = "Serverd02",
                                TargetHostUserName = @"corp\username",
                                TargetPassword = @"password",
                                TargetExecutableWithParams = @" E:\D01\Home.Framework.JobExecutionEngine\Home.Framework.JobexecutionEngine.exe 99999  1"};

  string value = task.DoWork();
  Assert.IsNotNull(value);
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
bhushanvinay
  • 449
  • 1
  • 5
  • 21
  • what if you change string quotedArgs = string.Format("\\\\{0} -u {1} -p {2} {3} " to string quotedArgs = string.Format(@"{0} -u {1} -p {2} {3} " or string quotedArgs = string.Format("{0} -u {1} -p {2} {3} "@TargetHostName,@TargetHostUserName,@TargetPassword,TargetExecutableWithParams4 – MethodMan Jan 04 '12 at 19:35
  • I would recommend using `Path.Combine(String, String)`, specifically for your `string psExecUtility =` code. – Erik Philips Jan 04 '12 at 19:44
  • The problem is with the processInfo.UseShellExecute = false if we comment out the bits and ignore the StanderdError and StandardOutput it works fine, only when we want to read it it just dies a death. Saying that it works very intermitently. so this may be a issue with the Win7 64 bit shell. – bhushanvinay Jan 05 '12 at 16:17
  • any solution about it ? what about http://www.autoitscript.com/forum/topic/42368-psexecsvcexe-problem/ ? echo command can hang psexec ? – Kiquenet Jan 18 '12 at 07:55

1 Answers1

0

basically replace where you have "\" double slashes with @ in front of the variable for example

what if you change string 
quotedArgs = string.Format("\\\\{0} -u {1} -p {2} {3} " 

to read like this
string quotedArgs = string.Format(@"{0} -u {1} -p {2} {3} " or

string quotedArgs = string.Format("{0} -u {1} -p {2}{3} ",
@TargetHostName,
@TargetHostUserName,
@TargetPassword,
@TargetExecutableWithParams4

also replace string psExecUtility = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\PsTools\psExec.exe";

to something like this

string psExecUtility =  Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "@PathandExName 

//which should be a var to this "\PsTools\psExec.exe";

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • That did not work. Tried all combinations but dosnt work. when with the i change it to string.Format("{0} -u {1} -p {2}{3} ", i get a error that parameter is invalid. changing the I did the change to do a Path.Combine also. nothing seems to work. – bhushanvinay Jan 05 '12 at 11:54