2

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?

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

3 Comments

There are no scripts involved here. I just want the interactive user input/output.
Try looking at the placeholder function Out-Default. Run: out-default -? for more information.
out-default seems to work okay. How about the command itself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.