小编Nic*_*k A的帖子

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

我正在从C#应用程序运行Powershell测试脚本.由于cmdlet错误导致pipe.Invoke()抛出异常,脚本可能会失败.

我能够捕获关于异常所需的所有信息,但我希望能够显示脚本的输出到那时为止.我没有运气,因为抛出异常时结果似乎为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?
}
Run Code Online (Sandbox Code Playgroud)

c# powershell powershell-1.0

11
推荐指数
2
解决办法
3万
查看次数

标签 统计

c# ×1

powershell ×1

powershell-1.0 ×1