捕获PowerShell的输出在C#Pipeline.Invoke后抛出 [英] Capturing Powershell output in C# after Pipeline.Invoke throws

查看:1265
本文介绍了捕获PowerShell的输出在C#Pipeline.Invoke后抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从C#应用程序一个PowerShell测试脚本。该脚本可能会失败,因为一个坏的小命令导致pipe.Invoke()抛出异常。

我能捕捉到我需要的有关异常的所有信息,但我希望能够到这一点显示该脚本的输出。因为结果出现时抛出一个异常为空我没有任何运气。

有我丢失的东西?谢谢!

  m_Runspace = RunspaceFactory.CreateRunspace();
m_Runspace.Open();
管线管= m_Runspace.CreatePipeline();
pipe.Commands.AddScript(File.ReadAllText(脚本文件));
pipe.Commands.Add(赛字符串);
尝试{
   结果= pipe.Invoke();
}
赶上(System.Exception的)
{
   m_Runspace.Close();
   //我怎样才能到异常之前自带PowerShell的输出?
}


解决方案

我结束了使用的解决方案是实现我们自己的PSHost处理PowerShell中的输出。这样做的最初信息来自<一个href=\"http://community.bartdesmet.net/blogs/bart/archive/2008/07/06/windows-powershell-through-ironruby-writing-a-custom-pshost.aspx\">http://community.bartdesmet.net/blogs/bart/archive/2008/07/06/windows-powershell-through-ironruby-writing-a-custom-pshost.aspx在构建一个定制的PS主机一节。

在我的情况下,它也需要使用自定义PSHostRawUserInterface为好。

下面是一个什么样做了简要概述。我只列出我实际上的形式实现的功能,但有许多是只包含抛出新NotImplementedException();

 私有类myPSHost:PSHost
{
   (同上面的链接中提到的)
}
私有类myPSHostUI:PSHostUserInterface
{
   私人myPSHostRawUI rawui =新myPSHostRawUI();   公共覆盖无效写入//所有变化
   公众覆盖PSHostRawUserInterface RawUI {{返回rawui; }}}
私有类myPSHostRawUI:PSHostRawUserInterface
{
   公众覆盖ConsoleColor ForegroundColor
   公众覆盖ConsoleColor BackgroundColor中
   公众覆盖缓冲区大小大小
}

I'm running a Powershell test script from a C# application. The script can fail due to a bad cmdlet which causes pipe.Invoke() to throw an exception.

I'm able to capture all the information I need about the exception, but I'd like to be able to display the script's output up to that point. I haven't had any luck since results appears to be null when an exception is thrown.

Is there something I'm missing? Thanks!

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?
}

解决方案

The solution I ended up using was to implement our own PSHost to handle PowerShell's output. The initial information for this came from http://community.bartdesmet.net/blogs/bart/archive/2008/07/06/windows-powershell-through-ironruby-writing-a-custom-pshost.aspx in the "Building a custom PS host" section.

In my case it did require using a custom PSHostRawUserInterface as well.

Here's the quick overview of what was done. I've only listed the function I actually implimented, but there's many that are just contain throw new NotImplementedException();

private class myPSHost : PSHost
{
   (Same as what the above link mentions)
}
private class myPSHostUI : PSHostUserInterface
{
   private myPSHostRawUI rawui = new myPSHostRawUI();

   public override void Write // all variations
   public override PSHostRawUserInterface RawUI { get { return rawui; } }

}
private class myPSHostRawUI : PSHostRawUserInterface
{
   public override ConsoleColor ForegroundColor
   public override ConsoleColor BackgroundColor
   public override Size BufferSize
}

这篇关于捕获PowerShell的输出在C#Pipeline.Invoke后抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆