[英]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. 我正在创建一个PowerShell二进制模块,它使用第三方库(DLL),它输出到控制台。 So, basically I'd like my binary module to output both its own messages and the console output of that DLL.
所以,基本上我希望我的二进制模块输出自己的消息和该DLL的控制台输出。
Is it possible? 可能吗?
So, let's say the third-party API does the following: 所以,让我们说第三方API执行以下操作:
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!
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. 查看System.Console.SetOut()方法,将当前进程的stdout临时设置为可以读取的文本编写器。 You'll use Console.OpenStandardOutput() to reset the stdout back to the default after calling the DLL.
在调用DLL之后,您将使用Console.OpenStandardOutput()将stdout重置为默认值。 Look at the example on the SetOut topic page.
查看SetOut主题页面上的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.