我有一个场景,我需要将 powershell 命令的命令和输出转发到另一个进程进行日志记录和处理。
我希望这个控制台尽可能接近 powershell,因此不希望将它简单地托管在另一个控制台程序中。
还有 start-transcript,但这似乎是缓冲的。我需要逐行获取控制台输出。
如果有办法让我在 System.Management.Automation.Internal.Host.InternalHostUserInterface 调用中的 WriteLine 期间在 powershell 主机上设置一个委托,那也是可行的。
这样做的最佳方法是什么?
更新(2012 年 5 月 29 日):我愿意不遗余力地做到这一点:
FieldInfo internaluiref = host.GetType().GetField("internalUIRef", BindingFlags.NonPublic | BindingFlags.Instance);
object valuecontainer = internaluiref.GetValue(host);
FieldInfo objectrefvalue = valuecontainer.GetType().GetField("_oldValue", BindingFlags.NonPublic | BindingFlags.Instance);
PSHostUserInterface internalHostUserInterface = (PSHostUserInterface)objectrefvalue.GetValue(valuecontainer);
FieldInfo externalUI = internalHostUserInterface.GetType().GetField("externalUI", BindingFlags.NonPublic | BindingFlags.Instance);
PSHostUserInterface externalHostUserInterface = (PSHostUserInterface)externalUI.GetValue(internalHostUserInterface);
HostUserInterfaceProxy indirector = new HostUserInterfaceProxy(externalHostUserInterface);
externalUI.SetValue(internalHostUserInterface, indirector);
这使我能够获得分类的输出流信息。
任何想法如何获得输入?