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

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

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.

Cannot redirect EXE output to file

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

How to force invoked command's STDOUT into invoking cmd.exe's STDOUT?

Context: Oracle Enterprise Manager has a feature to "execute host command." If into that feature I enter "dir c:\temp" then the output window echos the command and then shows a directory listing. If into that feature I enter "powershell dir c:\temp" the output window shows only the echo'd command. No directory listing. If on the target machine I enter those two commands in both cases I get the echo'd command followed by a directory listing.
I hypothesize that what I see in the cmd.exe window on the client blends two stdout streams: one from the cmd.exe itself and one from the invoked process (powershell dir c:\temp). The Oracle thing seems to recognize only the cmd.exe's stdout.
Is there some way I can force the stdout from the invoked process to be in the cmd.exe's stdout stream so that Oracle will recognize it and the thing I am trying to build will work?
I don't think you can directly pipe the output from one program back into STDOUT of a parent cmd.exe - assuming that is what Oracle is doing at some level.
That being said, you could try something clever like the following:
cmd /c "powershell -Command ""& echo Hello" > %TEMP%\a.txt & TYPE %TEMP\a.txt
Basically this is capturing the output from PowerShell, placing it in a temporary file, then dumping that file back onto STDOUT in cmd.exe. A nice touch would be cleaning up the temp file with a & DEL %TEMP%\a.txt on the end of the command.
You will probably need to toy around with the command line to account for any quirks in how Oracle is passing things along - my guess is that it is invoking cmd.exe /c directly so you can probably leave that part off.

Extra character added in batch script

So, I think I may be going insane. This batch script:
#Echo on
dir > dir.txt
generates the following on the console it is run from:
dir 1>dir.txt
I expected to see simply dir > dir.txt. Any ideas why this is happening? This is on Windows XP SP2 in the standard command prompt.
The 1 is the file descriptor for standard output. Therefore, these two commands are equivalent.
As a side note, you can redirect errors by redirecting descriptor 2, like this:
myCommand 1>goodoutput.txt 2>errors.txt
There's a nice summary of what you can do with redirection here.
Your redirection operator (>) is essentially sending your command output to the stdout (standard output). "1" is the stdout handler.
You can also pipe to the stderr (error output); like in UNIX by using the "2" handler.
e.g
myprogram.exe >> myoutput.txt 2>&1
For more information, see Command Redirection

Stdin to powershell script

I have a service running that can invoke an external process to modify a text stream before it is returned to the service. The text stream is handed from the service to the external process on stdout and the modified result is read from the service on stdin. The external process (command) can in other words be used as a text "filter". I would like to use a powershell script to modify the text stream. I can successfully launch a script from the service on win 2008r2 using the command "powershell -executionpolicy bypass -noninteractive ./myscript.ps1".
I can make the script return text to the service on stdout using the write-host cmdlet. My problem is that I can't find a way to read the text on stdin in the script. Read-host doesn't seem to work as it requires an interactive shell.
I would like to avoid writing the stdout from the service to a tmp file and read that file in the script as the service is multithreaded (can launch more than one external command at a time) and tmp file management (locking, unique filenames etc) is not desired.
Is this possible or should I use for example Perl for this? Powershell seems compelling as it is preinstalled on all my win 2008 machines.
Just a guess - I would have a look at [Console]::In | gm -static.