Cannot redirect EXE output to file - powershell

I have an exe program I'm running on Windows 10 using PowerShell. When I run it, I get the following output.
> .\Program.exe
Unlocked level 7/10
When trying to redirect all output or just stdout to a file, the program stops giving output. For example
PS > .\Program.exe > .\out.txt
PS > cat .\out.txt
PS >
I did not write the program but what I know is that it was written in C++.
Is there any trick to get the output into a file? I tried running in python and writing output to a file, running in python without fetching the output and redirecting, running from another powershell, and lots of other combinations but they don't seem to be working. Also, when running from Git Bash, I get no output at all.
I was thinking about some checks on the descriptors but I'm not sure since I don't have the source code, only the asm code

It looks like Program.exe is actually generating an error, and not output, first commenter is trying to get you to see that, but not really explaining that part...
(NOTE: You aren't actually using any powershell besides an implied "Invoke-Expression")I think you might be dealing with STDERR vs. STDOUT, when I invoke reg.exe in that fashion from powershell I get no output to the text file. This is because the text I was seeing was an error message ( Contents of STDERR ) from reg.exe, not the output ( contents of STDOUT ) from the command. When I passed proper parameters to it ( reg query HKLM\Software\Microsoft > C:\Users\foo\Documents\foo.txt) it wrote the Contents of STDOUT to the text file instead of the screen.
Here is an article that explains it better than I just did:
https://support.microsoft.com/en-us/help/110930/redirecting-error-messages-from-command-prompt-stderr-stdout

Related

How can I write to file descriptor 3 via pwsh on linux

I'm using pwsh on linux to run some specific powershell modules.
I want to output the data received to file descriptor 3. I want data on file descriptor 3 as Powershell doesn't respect the convention that stdout is data and stderr is logging. I want file descriptor 3 to serve as our "data" file descriptor.
End goal is to be able to do something like this as we wrap this powershell call in Python and we'll redirect the file descriptor 3 data ourselves
pwsh -f script.ps1 3>data
PowerShell has no built-in way to output to streams other than stdout (1) and stderr (2) (more on that below.
Conceivably, you can roll your own output behavior with .NET API and/or P/Invoke calls, but that would be cumbersome.
However, this may not be required:
While it is true that - unfortunately - PowerShell by default sends output from all its output streams to stdout - see GitHub issue #7989 - you can redirect PowerShell error-stream to stderr, if you apply a 2> redirection on the caller's side.
The following call, e.g. from bash, demonstrates this:
# Prints just 'hi', because the error-stream output was
# redirected to stderr thanks to 2>, and due to targeting /dev/null, suppressed.
pwsh -noprofile -c '"hi"; Write-Error no!' 2>/dev/null
The downside is that if you want to print the stderr output too, you must capture it in a file and print it afterwards, which means that it (a) won't appear at the time it is being produced and (b) therefore won't be properly interleaved with any stdout output.
As an aside:
The current behavior of the PowerShell CLI (as of v7.2.x) is unfortunate not only with respect to how output streams are mapped, but also because it loads profile files by default.
There was talk about providing a separate CLI to address the latter problem, in the context of which the stream-mapping behavior could be fixed too, but nothing has happened so far: see GitHub issue #8072.

how to redirect output of application in command window, which started with affinity?

the following command starts the C++ application in new command window with process affinity set to 0xF.
start /affinity F test.ext arg1 arg2
But the above command opens a new cmd window and closes immediately when test.exe ends. I tried the following to get output but it doesn't do anything.
start /affinity F test.ext arg1 arg2 ^> out.txt
I appreciate if you know how to do this on powershell.
Thanks
About Redirection
The PowerShell redirection operators are as follows, where n
represents the stream number. The Success stream ( 1 ) is the default
if no stream is specified.
Operator Description
Syntax
> Send specified stream to a file. n>
>> Append specified stream to a file. n>>
>&1 Redirects the specified stream to the Success stream. n>&1
# Examples
# Example 1: Redirect errors and output to a file
dir 'C:\', 'fakepath' 2>&1 > .\dir.log
This example runs dir on one item that will succeed, and one that will
error.
It uses 2>&1 to redirect the Error stream to the Success stream, and >
to send the resultant Success stream to a file called dir.log
# Example 2: Send all Success stream data to a file
.\script.ps1 > script.log
This command sends all Success stream data to a file called script.log
Also, a possible duplicate of the following
How to pipe all output of .exe execution in Powershell?
How to redirect the output of a PowerShell to a file during its execution
See also
Running external commands, can or will require special
consideration.
PowerShell: Running Executables
Solve Problems with External Command Lines in PowerShell
Top 5 tips for running external commands in Powershell
Using Windows PowerShell to run old command line tools (and their
weirdest parameters)
Execution of external commands in PowerShell done right
Part 1
Part 2
Part 3
Quoting specifics

cvs annotate and standard error redirection

First, some background. I'm trying to use cvs annotate within a Perl script invoked from a ksh command line to find out who's using bad hex constants in their source code and dole out justice appropriately. I'd like to use stdout for this program strictly for my own structured output, so I can pipe it to a file or to other programs. But, every call to cvs annotate results in a diagnostic message being printed to the console. It takes the form of:
Annotations for <filename>
***********
It's mucking up my stdout! I played around with annotate on the command line, and I figured that these diagnostic messages were coming from stderr, because running this command directly in ksh:
cvs annotate <filename> 1>yay.txt 2>boo.txt
correctly puts the desired annotated output into yay.txt and the diagnostics into boo.txt. However, things get weirder when I try to run this from within a perl script using backticks.
$muhstring = `cvs annotate $filename 2>boo.txt`;
The desired annotated output does appear in $muhstring, but the diagnostics still get printed to the command line, and boo.txt is created as an empty file. There's an admittedly old post on perlmonks.org that says this syntax should work as written. So, what's the deal? Is this a CVS quirk, have I misread the post, or have things changed since 1999? Or is it something else?
Edit: So, it is not, in fact, mucking up stdout. I tried this in ksh:
perl findbadhexowners.pl badhex.txt > out.txt
and the diagnostic messages still printed to the console, but out.txt only contains the annotations. This is fine for my purposes, and I can continue with other code.
That said, it seems like Perl and cvs specifically are interacting a little strangely, especially considering that redirection in ksh works fine and redirection in Perl for other commands like cd works fine - for example, in Perl
`cd nonexistentDir 1>stdout.txt 2>stderr.txt`;
gives the expected output in both stdout.txt and stderr.txt. It's only using cvs annotate from within Perl that produces this problem.

How to write Command Prompt to a file AND to the screen at the same time

I am trying to write the output of a powershell command to a file. I have completed this:
powershell psake "Dev08, Deploy-things" >> C:\Deployments\DeploymentLog.txt 2>&1
This is dumping all the screen output to a File... GREAT!!
NOW, I also want it to dump to the console screen too. I have used the argument >CON
but it will dump everything to the screen, but not the file.
powershell psake "Dev08, Deploy-things" >> C:\Deployments\DeploymentLog.txt 2>&1 >CON
Maybe >CON is not the way to go.. Here is a test I am using:
dir > c:\test\directory.txt 2>&1
Anyone got an idea on how to do this?????
Instead of having CMD output the file, let your Powershell command do it with the Tee-Object cmdlt.
So for your case, something like:
powershell ^& "'C:\Program Files\7-Zip\7z.exe'" ^| tee B:\test.log
Note that the ^ is required before | so that way it passes it to Powershell and isn't interpreted literally.
Well, as it turns out, I cannot find anything that says you can write to 2 different outputs in Command Prompt. Once you redirect the output to a file, you redirect it. That's it. I cannot find any way to write to the screen as well using commands. SO, the solution that I used was to download "TailXP". Install it on the box and configure it to point to the file i am writing. This will read the file as it's being generated and write it to it's own console screen. It served our purpose and I have it all wrapped up in a .cmd file.

Redirecting a .bat-file-containing-executable's stdout to a file

I have a batch file that calls an executable program. The program (compiled C code) generates some output to stdout. The batch echos some output as well. When running the bat, I use redirection (>) to get the text to a file.
mybat.bat contains:
myprog.exe arg1 %1 arg3
echo Done
then, at the console:
C:\> mybat arg2 > log.txt
The problem is that in log.txt I get only the output of the echo Done command and not the output of myprog.exe. Without the redirection, I get the expected output on the screen.
Note: Under Windows XP
Update: this gets even weirder. When running myprog.exe from the command prompt, I get the expected output to the console. Then, when redirecting its output to log.txt, the file is empty! The printing is done using fprintf(stdout, "...") or fprintf(ofp, "...") where ofp is assigned: FILE *ofp = stdout;.
Further investigation: it seems like the fprintf(stdout... lines where redirected, while the fprintf(ofp... are not (yes, the pointer is assigned correctly). I also found that the program crashes at somepoint (at a call to feof()). So, my conclusion is that due to the abnormal termination of the program, the standard output buffers were not written to the file. HOWEVER - this happened only for the lines that use the pointer. I guess that these lines have shorter output, so the flush frequency is lower (I used the stdout line to print a deliberate help message).
Once solving the crash problem, the data is now redirected to the log file. Thanks for your help.
Try redirecting both stdout and stderr to the log file.
mybat arg2 1>&2> log.txt
Try this:
cmd /c "mybat.bat arg2" > log.txt