我從C#應用程序運行Powershell測試腳本。由於導致pipe.Invoke()拋出異常的錯誤cmdlet,該腳本可能會失敗。在Pipeline.Invoke後捕獲C#中的Powershell輸出
我能夠捕獲我需要的有關異常的所有信息,但我希望能夠顯示該腳本的輸出。我沒有任何運氣,因爲在引發異常時結果似乎爲空。
有什麼我失蹤了嗎?謝謝!
m_Runspace = RunspaceFactory.CreateRunspace();
m_Runspace.Open();
Pipeline pipe = m_Runspace.CreatePipeline();
pipe.Commands.AddScript(File.ReadAllText(ScriptFile));
pipe.Commands.Add("Out-String");
try {
results = pipe.Invoke();
}
catch (System.Exception)
{
m_Runspace.Close();
// How can I get to the Powershell output that comes before the exception?
}
不錯,我正在尋找。謝謝。 – 2012-07-23 08:34:34