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

Tee object can now store received error records to a variable. #21565

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JamesWTruher
Copy link
Member

This addresses an aspect of issue #5184

PR Summary

This adds the parameter -ErrorRecordVariable (Alias ERV) to Tee-Object
if Tee-Object receives an ErrorRecord in the input stream and -ErrorRecordVariable is included,
Tee-Object will assign those error records to the variable. This enables the following scenario:

PS> Get-Item $PSHOME,doesnotexist 2>&1 | Tee-Object -var out -ERV ee

    Directory: /Users/james/src/github/forks/jameswtruher/PowerShell-1/src/powershell-unix/bin/Debug/net9.0/osx-x64

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwxr-xr-x      james staff        5/1/2024 11:34         9024 publish

PS> $out

    Directory: /Users/james/src/github/forks/jameswtruher/PowerShell-1/src/powershell-unix/bin/Debug/net9.0/osx-x64

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwxr-xr-x      james staff        5/1/2024 11:34         9024 publish

PS> $ee
Get-Item: Cannot find path '/Users/james/src/github/forks/jameswtruher/PowerShell-1/src/Microsoft.PowerShell.Commands.Utility/commands/utility/doesnotexist' because it does not exist.
PS>

It works with file, literalfile, and variable parameter sets.
This works with native executables as well, as when stderr is written, we create an error record.
Errors created by Tee-Object are emitted in the normal way, it is only the input object which is inspected and handled by this new behavior.
Note: the 2>&1 is required to ensure that the error stream is emitted into the output stream

PR Context

PR Checklist

Tee object can now store error records to a variable.
@JamesWTruher JamesWTruher changed the title Tee object can now store recieved error records to a variable. Tee object can now store received error records to a variable. May 1, 2024
@SteveL-MSFT SteveL-MSFT added the PowerShell-Docs needed The PR was reviewed and a PowerShell Docs update is needed label May 1, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label May 1, 2024
Add a native executable test.
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label May 1, 2024
Copy link
Member

@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Jaykul
Copy link
Contributor

Jaykul commented May 2, 2024

From the example, you seem to have eaten the error!?

Tee-Object should still pass through errors, surely.

}

It "Positive File/ErrorRecordVariable Test" {
$results = Get-Item $PSHOME,doesnotexist1,$PWD,doesnotexist2 2>&1 | Tee-Object -File $tempFile -ErrorRecordVariable errVar
Copy link
Contributor

@Jaykul Jaykul May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test where you Tee a second time to prove the error still passes through and is output?

@Jaykul
Copy link
Contributor

Jaykul commented May 2, 2024

Incidentally, is it even possible to get output in -OutVariable or -ErrorVariable from this command?

I mean, even when I do, say | Tee-Object -File Variable:\Foo or | Tee -File NoSuchDrive:\log.txt I do not get an error.

It's weird to me that it has two parameters for output and error variables, but the one we all EXPECT ... does not work. It's pretty confusing.

@SteveL-MSFT
Copy link
Member

Incidentally, is it even possible to get output in -OutVariable or -ErrorVariable from this command?

I mean, even when I do, say | Tee-Object -File Variable:\Foo or | Tee -File NoSuchDrive:\log.txt I do not get an error.

It's weird to me that it has two parameters for output and error variables, but the one we all EXPECT ... does not work. It's pretty confusing.

If you redirect stderr to stdout and pipe to Tee-Object, you'll get both into the output stream and can do whatever you want. We thought the ask was to be able to handle stderr separately thus the new variable to de-multiplex it. Certainly you could do this yourself just by looking at the type of object and this was a convenience feature.

@mklement0
Copy link
Contributor

mklement0 commented May 17, 2024

@SteveL-MSFT, while I can appreciate what this PR is trying to achieve, I think it is ultimately the wrong way to go about it, especially given that it requires modifying the call that provides the input (2>&1) in order to work meaningfully.

  • Redirecting to a variable should be possible #20381 is a great extension to how > and >> work, but it is currently limited to consuming the targeted stream; that is, unlike with the common -*Variable parameters, the stream isn't passed through.

  • Extending the 2> variable:foo syntax to optionally support pass-through - which you yourself suggested and as previously discussed, where I floated the idea of 2>| variable:foo - strikes me as the better alternative to extending Tee-Object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PowerShell-Docs needed The PR was reviewed and a PowerShell Docs update is needed Review - Needed The PR is being reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants