Yan Sklyarenko
Yan Sklyarenko

Reputation: 32240

How to capture the console output of the external DLL in a PowerShell binary module?

I'm creating a PowerShell binary module, and it uses the third-party library (DLL), which outputs to the console. So, basically I'd like my binary module to output both its own messages and the console output of that DLL.

Is it possible?

So, let's say the third-party API does the following:

public static void SomeMethod() {
  ...
  Console.Write("Extracting the file...");
  ...
}

The binary module calls it:

protected override void ProcessRecord() {
  ...
  this.WriteObject("Hello!");
  SomeClass.SomeMethod();
  this.WriteObject("Goodbye!");
}

The output I see is:

Hello!
Goodbye!

What I'd like to see is:

Hello!
Extracting the file...
Goodbye!

Upvotes: 3

Views: 857

Answers (1)

Keith Hill
Keith Hill

Reputation: 201652

Have a look at the System.Console.SetOut() method to temporarily set the current process's stdout to a text writer that you can read from. You'll use Console.OpenStandardOutput() to reset the stdout back to the default after calling the DLL. Look at the example on the SetOut topic page.

Upvotes: 5

Related Questions