2012-12-18 68 views
4

我使用的是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; 
     } 
+0

請顯示您用於讀取過程輸出(您重定向)的代碼。 – usr

+0

更新了原始問題。 –

+1

以完全相同的方式從cmd提示符啓動psexec時,是否會發生同樣的問題? (據我所知,你確實做得很對 - 我過去做過類似的事情)。 – usr

回答

0

這是不是一個真正的答案,但我遇到了一個非常類似的問題。我在同一個域中運行,但看到與上述相同的結果。

每當我從C#代碼或PowerShell運行PsExec時,該過程似乎都會掛起。當我觀看遠程服務器時,我看到PSEXESVC進程啓動,以及我試圖在遠程服務器上運行的命令。該命令正常退出,但PSEXESVC似乎掛起。

遠程命令我傳遞的是:cmd /c dir E:\temp

我從LINQPad與設置爲false,並打開它返回預期的輸出命令提示符下沿重定向設置爲false CreateNoWindow測試這一點。只要我再次嘗試捕獲輸出,過程就會回到掛起狀態。

我知道這用於C#使用相同的代碼一年或2年前工作,所以我不知道什麼已經改變,導致它現在掛起。