2016-03-15 84 views
1

我想在使用PSEXEC工具的远程计算机上运行命令,它使用命令提示符成功运行,但它在显示以下内容的asp.net项目中失败输出错误。PSEXEC通过命令提示符成功运行,但在ASP.Net/C中失败#

PSEXEC V2.11 - 执行过程远程版权(C)2001至2014年马克 Russinovich介绍的Sysinternals - www.sysinternals.com

句柄无效。

连接到lnxdevx ...

开始对lnxdevx PSEXESVC服务...

与lnxdevx PSEXEC服务连接...

错误派生会话密钥:。

这是我的示例c#代码。

Process p = new Process(); 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.RedirectStandardError = true; 
p.StartInfo.RedirectStandardInput = true; 
p.StartInfo.FileName = @"C:\Windows\System32\PsExec.exe"; 
p.StartInfo.Arguments = "-u xxxxxx -p xxxxxxxxxx \\\\lnxdevx -i -d -w \"C:\\DIRECTORY\" cmd.exe /C dir"; 
p.Start(); 
string output = p.StandardOutput.ReadToEnd(); 
string errormessage = p.StandardError.ReadToEnd(); 
p.WaitForExit(); 
+0

你有没有尝试将UseShellExecute更改为true? (这似乎是默认) –

回答

0

的问题是,你不必打开psexec,但cmd,并从那里运行PSEXEC。

你正在试图做的编码是错误的也一样,你需要使用

System.Diagnostics.Process process = new System.Diagnostics.Process(); 
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
startInfo.FileName = "cmd.exe"; \\if it is in System32 
startInfo.Arguments = "psexec -u xxxxxx -p xxxxxxxxxx \\machine *your stuff*"; 
process.StartInfo = startInfo; 
process.Start(); 

我没有测试,但我敢肯定,这将工作:)

+0

谢谢路易斯,你救了我的一天。 –

相关问题