PSExec挂起运行测试但在调试会话上运行良好

时间:2012-01-04 19:08:30

标签: c# mbunit psexec

当我在调试模式下运行代码时,测试完成,当我在运行测试中运行时,ps exec似乎挂起并且永远不会返回我将kill放到位但是它从未完成。

为什么这会在调试时完成,但不会在运行模式下完成。

这里有什么傻事我错过了吗?

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]
[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);
}

1 个答案:

答案 0 :(得分:0)

基本上替换变量前面带有@的双斜杠的位置,例如

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

这样读     string quotedArgs = string.Format(@“{0} -u {1} -p {2} {3}” 或

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

也替换     string psExecUtility = Path.GetDirectoryName(Assembly.GetExecutingAssembly()。Location)+“\ PsTools \ psExec.exe”;

这样的事情

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

//应该是这个“\ PsTools \ psExec.exe”的var;