我使用的是C#.NET 4的過程與代碼PSEXEC 1.98(從C#程序生成)跨域
var startInfo = new ProcessStartInfo(psExecLocation)
{
Arguments = string.Format(@"\\{0} -accepteula -u {1} -p {2} {3}", serverName, username, password, command),
RedirectStandardError = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
StandardErrorEncoding = Encoding.UTF8,
StandardOutputEncoding = Encoding.UTF8,
UseShellExecute = false
};
使用PSEXEC拉開PSEXEC運行7Zip的時候從來沒有完成的時候,我裏面的一樣正常工作域作爲我的開發機器(對於我所做的所有調用,這不僅僅是我遇到的這個問題)以及執行簡單的DOS命令(rmdir和mkdir)跨域時(因爲我在調用之前正在執行它們)。
當我有它運行下面的命令,但是:
C:\ 7Zip的\ 7za X 「C:[文件路徑包括空格] \ __ __ STAGING \ App01 [幹線 - STAGE_20121217.2] .7z壓縮」 - o「C:[文件路徑包括空格] \ __ STAGING __」-y -aoa
它永遠不會返回。我可以看到它開始返回數據(我正在使用進程重定向輸出,並開始返回它所做的),但之後它突然停止並且不做任何事情。它在撥打電話後永不返回
process.WaitForExit();
奇怪的是,它成功地完成了。我可以將RDP放入我們正在提取文件的服務器中,並且可以在那裏看到這些文件,這只是PsExec不會返回任何內容。我已經嘗試刪除-y -aoa開關並移動事物的順序,似乎沒有任何工作。僅供參考,我們試圖提取的服務器是Windows Server 2003版和Windows Server 2008 R2版。
至於問,這裏正是我在做什麼打開使用PSEXEC過程:
try
{
using (var process = new Process())
{
var startInfo = new ProcessStartInfo(psExecLocation)
{
Arguments = string.Format(@"\\{0} -accepteula -u {1} -p {2} {3}", serverName, username, password, command),
RedirectStandardError = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
StandardErrorEncoding = Encoding.UTF8,
StandardOutputEncoding = Encoding.UTF8,
UseShellExecute = false
};
process.StartInfo = startInfo;
var processOutput = "";
Action<object, DataReceivedEventArgs> action = (obj, eventArgs) =>
{
if (string.IsNullOrWhiteSpace(eventArgs.Data) == false)
processOutput += string.Format("{0}\r\n", eventArgs.Data);
};
process.OutputDataReceived += new DataReceivedEventHandler(action);
process.ErrorDataReceived += new DataReceivedEventHandler(action);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
// There appears to be a fault in PsExec that has everything go to error even if it is output. This gets around that
output = "";
if (string.IsNullOrWhiteSpace(processOutput) == false)
{
output = processOutput;
// error code 0 means it worked/succeeded. Any other "error code" means something failed
var loweredOutput = processOutput.ToLower().Replace("\r\n", "").Trim();
return loweredOutput.EndsWith("error code 0.") || loweredOutput.EndsWith("error code 0");
}
// if it got here, psexec didn't return anything. That SHOULD never happen (emphasis on should)
return false;
}
}
catch (Exception ex)
{
Logging.Logger.LogError(ex);
output = "";
return false;
}
請顯示您用於讀取過程輸出(您重定向)的代碼。 – usr
更新了原始問題。 –
以完全相同的方式從cmd提示符啓動psexec時,是否會發生同樣的問題? (據我所知,你確實做得很對 - 我過去做過類似的事情)。 – usr