Reputation: 535
I have a scenario where I need to forward the command and output of a powershell command to another process for logging and processing.
I would like this console to be as close to powershell as possible, so simply hosting it in another console program is undesirable.
There's also start-transcript, but that seems to be buffered. I need to get the console output on a line-by-line basis.
If there's a way for me to set a delegate on the powershell host during WriteLine in the System.Management.Automation.Internal.Host.InternalHostUserInterface calls, that would be workable as well.
What's the best way to go about doing this?
Update (2012 May 29): I'm willing to go to absurd lengths to do this:
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);
This allows me to get categorized output stream information.
Any ideas how I can get the input?
Upvotes: 2
Views: 516
Reputation: 52450
Don't confuse formatted output with raw output. Anything that reaches the host interface has been already been munged into a string. If you want the actual objects returned from a powershell script, take a look at my other answer here on how to stream output from a scriptblock executed in C#:
How to stream the output of a programmatically executed ScriptBlock?
Upvotes: 2