Redirect stdout from loaded assembly console.writeline
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Doubt if it’s a bug, limitation or a lack of enough knowledge:
I’m loading arbitrary .NET assemblies (which I have no control of their source code) and invoke them.
A lot of them are using console.writeline
(or similar functions), and I want to capture it, means redirecting it to a Powershell variable or even to a file, but nothing seems to catch this - it’s just printing it to the console.
Tried about million ways to overcome this unsuccessfully (> file.txt , out-string, out-variable, and so on…)
here is the code:
$file = "path\to\my\compiled\assembly"
$bytes = [System.IO.File]::ReadAllBytes($file)
$assembly = [Reflection.Assembly]::Load($bytes)
$params = @(,[String[]]@("some params"))
$assembly.EntryPoint.invoke($null, $params) # <-- I want to catch this output, into a variable or a file, but it keeps be printed to the screen.
Expected behavior
PS> $x = $assembly.EntryPoint.invoke($null, $params) | out-string
PS> $x
output
Actual behavior
PS> $x = $assembly.EntryPoint.invoke($null, $params) | out-string
PS> $x
nothing
Error details
No response
Environment data
Name Value
---- -----
PSVersion 5.1.19041.1682
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1682
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Visuals
No response
Issue Analytics
- State:
- Created a year ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Redirect Console.Write of specific assembly
1 Answer 1 · Console class has static method SetOut , which redirects output to StreamWriter object. · Write your own StreamWriter extension...
Read more >How to redirect/catch Console.WriteLine() output
Dear allIn my script I load the .NET assembly library "TagLib#", a library to access e.g. mp3-tags.If the the importend "Create"-function of ...
Read more >Customize output in hosted CLR (.NET) - Microsoft Q&A
Console.WriteLine uses the processes standard output file handle. so to capture all standard output for your process, you redirect standard ...
Read more >How to redirect standard output for a .NET console application
The Console. SetOut method is the most important method to remember if you want to redirect the standard output.
Read more >Console.SetOut(TextWriter) Method (System)
It replaces four consecutive space characters in a string with a tab character. To run it, you must supply two command line arguments....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can also set the console output to a stream like this:
@MartinGC94 Seems like it’s working, great catch! Thank you so much 😃