2016-03-15 46 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

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

相關問題