Skip to content
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

Redirection operator > should not be hardcoded to files #23794

Open
SteveL-MSFT opened this issue May 13, 2024 · 6 comments
Open

Redirection operator > should not be hardcoded to files #23794

SteveL-MSFT opened this issue May 13, 2024 · 6 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Language parser, language semantics

Comments

@SteveL-MSFT
Copy link
Member

Summary of the new feature / enhancement

Currently, > will internally call Out-File. Instead, it should probably call Out-Provider (or something like that) so that any provider can participate: 1 > variable:a.

Proposed technical implementation details (optional)

If a provider path isn't provided, default to filesystem

@SteveL-MSFT SteveL-MSFT added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels May 13, 2024
@rhubarb-geek-nz
Copy link

One can simply pipe through Set-Variable if you want that

PS>  'foo' | Set-Variable 'bar'
PS> echo $bar
foo

No?

@SteveL-MSFT
Copy link
Member Author

That works for output (or STDOUT), but we don't have 2| for error stream so you have to use 2> which only writes to files currently. Although we have #20381 which would allow redirection to a variable and during that code review I thought of this.

@rhubarb-geek-nz
Copy link

so you have to use 2> which only writes to files currently

If you use *>&1 you can filter for ErrorRecord in the success output. Example.

@SteveL-MSFT
Copy link
Member Author

SteveL-MSFT commented May 13, 2024

so you have to use 2> which only writes to files currently

If you use *>&1 you can filter for ErrorRecord in the success output. Example.

Yes, but sometimes you want (foo 2>variable:stderr) >variable:stdout

There's also #21565 for those that want tee-object for errors

@jborean93
Copy link
Collaborator

jborean93 commented May 13, 2024

I agree this would be great to have, I currently use the following to capture stdout/stderr as separate vars

$stdout = $null
$stderr = . { my.exe | Set-Variable -Name stdout } 2>&1 | ForEach-Object ToString

The ForEach-Object ToString is only needed for WinPS to capture stderr as a string and not the ErrorRecords in the stream. Having a way to do it directly with redirection would be really nice as I can avoid the above complexity.

@mklement0
Copy link
Contributor

@jborean93, the more succinct cross-edition formulation of your code is:

$stdout, [string[]] $stderr = (my.exe 2>&1).Where({ $_ -is [string] }, 'Split')

What #20381 now enables:

my.exe 1> variable:stdout 2> variable:stderr

Alternatively:

$stdout = my.exe 2> variable:stderr

That is, #20381 is sufficient for targeting variables via redirections, via the variable: drive.

The issue at hand is about generalizing this approach, allowing any provider's items as the target.

@SteveL-MSFT SteveL-MSFT added the WG-Language parser, language semantics label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Language parser, language semantics
Projects
None yet
Development

No branches or pull requests

4 participants