-
Notifications
You must be signed in to change notification settings - Fork 7.2k
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 should support redirecting to variable names or stream objects #5184
Comments
Personally, I think we should not enhance somecommand 2> variable:stderr 1> variable:stdout
We should also allow redirecting one stream to another stream beyond stdout like: somecommand 2>&3 |
@SteveL-MSFT For long running commands, this would make it look like powershell has hung. I think we need a layered approach where the lowest level converts output and errors to a tagged union of It also opens a path to handling more complex commands where there is mixed progress and verbose output in the different streams, and makes it quite easy to make a parser that interprets the output and direct it do the appropriate powershell stream. |
public class ProcessOutput {
private string _value;
public ProcessOutputKind Kind {get;}
public bool IsOutput => Kind == ProcessOutputKind.Output;
public string Error => Kind == ProcessOutputKind.Error ? _value : throw new InvalidOperationException();
public string Output => Kind == ProcessOutputKind.Output ? _value : throw new InvalidOperationException();
}
class ProcessEx {
public IEnumerable<ProcessOutput> Run(..., CancelationToken cancel){
...
foreach(processOutput item in someConcurrentQueueWithOutputAndErrors.Enumerate(cancel)) {
yield return processOutput;
}
}
private void OnError(...){
someConcurrentQueueWithOutputAndErrors.Add(new ProcessOutput(ProcessOutputKind.Error, theErrorString);
}
private void OnOutput(...){
someConcurrentQueueWithOutputAndErrors.Add(new ProcessOutput(ProcessOutputKind.Output, theErrorString);
}
} Something similar would make it easy to use from the PowerShell side. foreach($po in ProcessEx.Run(...)){
if ($po.IsOutput){
# parse output and split into Output and Progress
}
else {
# parse error and split into for example Verbose, Warning and Error
}
}
|
If we had the ability to capture in a variable while optionally also passing output through, then I think no lower-level approach is needed. I had previously suggested syntax However, additional syntax is needed for:
These two behaviors would bring the redirection operators on par with the |
P.S: Asking for the ability to capture stderr lines in a variable is the subject of #4332 |
It's too bad this relatively simple request got set aside for a more elegant but unachievable better idea ... |
I've queued this up for WG to discuss |
@PowerShell/wg-powershell-cmdlets discussed this, since stderr gets wrapped as an ls *foo* README.txt 2>&1 | Tee-Object -Variable stdout_variable -ErrorRecordVariable stderr_variable -ErrorVariable teeObjectErrors We would enhance |
Tee object can now store error records to a variable.
Tee object can now store error records to a variable.
Because there are many executables (mostly of linux origin) which use stderr to write status messages (in addition to errors), and rely purely on exit code to indicate whether there actually is an error or not ....
As a PowerShell scripter, I need to be able to run an executable and both stream the output for sake of status updates and capture the (stderr) output so I can report on it properly if there is an actual error.
Start-Process doesn't support this -- it only captures to file, and if it does, it doesn't output. Additionally, since output stream redirection is limited to redirecting to
stdout
or file, I cannot redirect error to verbose on CLI apps that consistently behave this way...My current company has contrived more than one complicated wrapper around
ProcessStartInfo
andDiagnostics.Process
to handle the output events and so on. In fact, it seems that every company I work with above a certain level of PowerShell maturity has their own customInvoke-Process
or other replacement for Start-Process to try and handle this.Start-Process should allow capturing and also outputting
Start-Process should allow capturing (and reading input from) variables instead of just files
The text was updated successfully, but these errors were encountered: