Tag: powershell v1.0

在Pipeline.Invoke抛出后在C#中捕获Powershell输出

我正在从C#应用程序运行Powershell测试脚本。 由于cmdlet错误导致管道失败,导致pipe.Invoke()抛出exception。 我能够捕获关于exception所需的所有信息,但是我希望能够显示脚本的输出到那时为止。 我没有运气,因为抛出exception时结果似乎为null。 有什么我想念的吗? 谢谢! 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? }