New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start-Process: allow redirecting std output/error to console stream #18026
Comments
Here i posted a workaround leveraging a temp file: aetonsi/pwsh__StartProcessWaitRedirectToStream |
What are you using start-process at all ? If it is a command line program start-process will (by default) run in a new window, not receive output and not wait for the program it has started to finish. You can specify Wait or NoNewWindow but you still need to work around the output not coming back
|
I need my command to be run in a separate window, that's why i resorted to |
Not without using dotnet but that’s the beauty of PowerShell, you can wrap all that in your own function to achieve what you want. |
Note that there's a previous discussion about generally allowing targeting variables in contexts where file paths are expected, so that This is not only more flexible than See:
|
@mklement0 Mmh i see, that would be cool. Not the same as what i was picturing ( |
@JamesWTruher I thought you had a proof-of-concept for supporting redirection to the variable provider? |
@SteveL-MSFT Indeed I do: #16497 |
That doesn’t really help here for Start-Process if I understand it correctly? You can already capture output with just invoking the exe today. Supporting Start-Process redirection to a var would be quite tricky unless it meant -Wait was always implicit as it would require a background task to be running which could mutate the var as it’s being read. |
I think in this case there is no output from Start-Process itself that can be redirected. It would need start-process to allow |
Yes but AFAIK you cannot start a process in a new window, wait for it and capture the output |
Why would you want to, that would mean you have a blank window with no output. When you redirect a std pipe in a process you cause that output to no longer appear. You would have to somehow split the output to go to a pipe you can capture but also have it output to the console at the same time which isn’t really easy to do at all. |
@jborean93, it's a good point that redirecting to variables could only work meaningfully with (possibly implied)
The original issue, #4332, was focused on In the context of (synchronous use of)
They're certainly not common uses cases, but two come to mind (in both cases,
|
@aetonsi I didn't think of this when you said to my suggestion before that you wanted it its own window. Why do do you want a blank window? |
The bottom line is: Are there legitimate use cases for synchronous use of |
It’s not pretty but you can do this today $stdout = $null
$stderr = . { my.exe args | Set-Variable stdout } 2>&1 | ForEach-Object ToString The ForEach-Object ToString` is only needed for WinPS because on the older versions these objects are error records and you want the string value instead.
I will concede this is a valid point, thank you I didn’t think off this.
I’m not sure why this needs Start-Process. You can certainly just do Still I feel like the better option is to add NoteProperties to the return object if |
Good point:
That's precisely the point: let's make it pretty (easy): # Capture stderr output in $stderr
$stdout = my.exe args 2>variable:stderr As for the least ugly version right now: $stdout, $stderr = (my.exe args).Where({ $_ -is [string] }, 'Split') But all of that relates to direct invocation, which I think is the primary use case for the proposed enhancement. I don't feel strongly about the I'm unclear on what you mean by adding NoteProperties. |
@mklement0 @jhoneill And now that i think about it, this can work only for streams 1/2, number 3-6 are unmanageable. Even with a syntactic sugar like |
@aetonsi, yes, when you call a script via the PowerShell CLI, the stdout and stderr streams are the only ones available, as with any child process. By default, PowerShell only sends its error stream to stderr (curiously, only when stderr is redirected), all others (success output, warnings, verbose messages, ...) are output to stdout (which is problematic - see #7989) The only way to distinguish between PowerShell's output streams via a CLI call would be to use In other words: this approach indeed would leave your window invariably blank, because all output would be communicated via stdout. |
Redirecting to variables would mean that the process would have to completely finish before you printed to console. Being able to redirect to the current runspace's streams would mean you could have streaming output. |
Summary of the new feature / enhancement
Hello.
I am trying to start a process in a new window with
Start-Process
and i'm-wait
ing for its execution.Then i need to process its output. I would like to do it all in memory, without writing to disk, as the output is confidential. Unfortunately at the moment this doesn't seem to be possible (correct me if i'm wrong) without resorting to .NET implementations like this...
Currently, this is the best you can do:
In this example,
powershell "read-host 'Enter string: '"
is my command with confidential output.-Wait
waits for my command to exit-RedirectStandardOutput
permits the redirection to a fileget-content
simply reads back that file.Proposed technical implementation details (optional)
A possible solution would be a parameter like
-RedirectStandardOutputToStream
/-RedirectStandardErrorToStream
Where
1
is the ID of the success stream.Or, more simply:
... with the corresponding
-GetStandardError
The text was updated successfully, but these errors were encountered: