PSExec在运行测试时挂起,但在调试会话中工作正常
问题描述:
当我在调试模式下运行代码时,测试完成,当我在运行测试中运行它时,ps exec似乎挂起并永远不会返回我将kill但也没有完成。PSExec在运行测试时挂起,但在调试会话中工作正常
为什么在调试时完成此操作,而不是在运行模式下完成。
这里有什么愚蠢的东西我失踪了吗?
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);
}
答
基本上替换,你必须 “\” 与@双斜线在变量前面例如
what if you change string
quotedArgs = string.Format("\\\\{0} -u {1} -p {2} {3} "
读取这样
串quotedArgs =的String.Format(@“{0 } -u {1} -p {2} {3}“ 或
string quotedArgs = string.Format("{0} -u {1} -p {2}{3} ",
@TargetHostName,
@TargetHostUserName,
@TargetPassword,
@TargetExecutableWithParams4
也替换 串psExecUtility = Path.GetDirecto ryName(Assembly.GetExecutingAssembly()。Location)+“\ PsTools \ psExec.exe”;
到这样的事情
string psExecUtility = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "@PathandExName
//这应该是一个变种这个 “\ PsTools \ psExec.exe”;
+0
这没有奏效。试过所有的组合,但dosnt工作。当我改变它为string.Format(“{0} -u {1} -p {2} {3}”,我得到一个错误,该参数是无效的。改变我做了更改做一个路径。结合也没有什么似乎工作。 – bhushanvinay 2012-01-05 11:54:38
如果将字符串quotedArgs = string.Format(“\\\\ {0} -u {1} -p {2} {3}”改为string quotedArgs = string.Format(@“{0} - ({{0} -u {1} -p {2} {3}“@ TargetHostName,@ TargetHostUserName,@ TargetPassword,TargetExecutableWithParams4 – MethodMan 2012-01-04 19:35:11
我建议使用'Path.Combine(String,String)',专门用于'string psExecUtility ='代码。 – 2012-01-04 19:44:16
问题是,如果我们注释掉这些位并忽略StanderdError和StandardOutput,processInfo.UseShellExecute = false好吧,只有当我们想读它时,它才会死亡,说它的工作非常复杂,所以这可能是Win7 64位shell的一个问题, – bhushanvinay 2012-01-05 16:17:18