Skip to content

A common -Tee parameter #20133

Open
Open

Description

@iRon7

Summary of the new feature / enhancement

A common -Tee parameter for outputting the processed item and passing through the original input item.

This feature request is a follow up of #19989
Currently:

  • The -PassThru switch is based on individual cmdlets and therefore isn't commonly available and consistently implemented.
  • The Tee-Object cmdlet is able to tee the current object but is limited to a specific output (or a new pipeline as purposed in Tee-Object -Process #20123)
    In my believe every (advanced) function/cmdlet that has a ValueFromPipeline input (and has no success stream pipeline output¹), should have (an easy standard way to implement) a common way to pass the current input item using a -Tee parameter.
  1. Any output passed to the success stream should be redirected to the information stream (similar to what usually happens at the end of pipeline) and the original input item/object should be placed back into the success stream. This allows me to do thinks along with (wishful thinking):
    ... | Format-Table -Tee | ...
    Or something more sophisticated with the current stage:
    ... | Foreach-Object { Write-Host "Processing: $($_.Name)" } -Tee | ....
    (I think this need a deeper thoughts as this opens the possibility to recurse a -Tee which has some similarities -and pitfalls?- with the common -PipelineVariable parameter)

Take e.g.: Powershell - Get IP Addresses for hostnames from File (more than 150K hostnames)

The Export-Csv appears not be written for the middle of the pipeline and doesn't support the -PassThru parameter:

Export-Csv: A parameter cannot be found that matches parameter name 'PassThru'.

But I see no reason why these type of cmdlets shouldn't have a possibility to pass the current input item.
Wishful thinking:

... | Export-Csv .\test.csv -Tee | Format-Table

This way you might avoid wrapping a new cmdlet pipelines to continue with the current input item.

Activity

  1. added
    Issue-Enhancement
    Needs-Triage
    on Aug 17, 2023
  2. changed the title [-]A common `-Reprocess` parameter for passing through the current input item.[/-] [+]A common `-Tee` parameter for passing through the current input item.[/+] on Aug 18, 2023
  3. changed the title [-]A common `-Tee` parameter for passing through the current input item.[/-] [+]A common `-Tee` parameter for outputing the processed item and passing through the original input item.[/+] on Aug 18, 2023
  4. changed the title [-]A common `-Tee` parameter for outputing the processed item and passing through the original input item.[/-] [+]A common `-Tee` parameter[/+] on Aug 18, 2023
  5. jhoneill commented on Aug 18, 2023

    @jhoneill
    1. Any output passed to the success stream should be redirected to the information stream

    See my response to #20123 - your way is a better to keep things out of the "success" pipeline than my addition of | Out-Null

    BTW You've edited the title to make the parameter name -Tee but haven't kept the body text in sync.

    I think this idea is evolving towards something workable, which the first versions of it were not, really. There are some other questions which need to be answered though

    1. What happens to errors in the "branch" do they go to error etc.
    2. A confirmation, or simply a long running process in the "branch" would probably pause the trunk (unlike the usual tee)
    3. Would a terminating error in the branch terminate the trunk ? (I haven't check what Tee does today if it can't write to the file given) - but potentially a command which makes the tee can have a -Catch scriptblock-parameter.

    Some of these things are easier to do with the functionality in another command - a bit like NOT having a -RedirectToFile common parameter - that route might be easier than the new common parameter way.

  6. iRon7 commented on Aug 18, 2023

    @iRon7
    Author

    James O'Neill (@jhoneill),

    Thanks for the response.

    BTW You've edited the title to make the parameter name -Tee but haven't kept the body text in sync.

    Sorry for that (I hope, I have updated all the old references by now). The request has evolved a few times and the original description is even a little bit outdated. Let me summarize the definition for a common -Tee parameter as I (currently) see it:

    Definition

    The Common -Tee parameter will:

    • Put the original input back on the pipeline (success stream), and
    • redirect any possible output to the host (information stream)
    Information ──►│ ──►              ─────► │ ──► Information
                   │     Cmdlet -Tee   ▲     │
        Success ──►│ ─┐             ? ─┘ ┌─► │ ──► Success
                   │  └──────────────────┘   │
    

    There are some other questions which need to be answered though

    Agree, I can't see this completely through and expect also some possible confusion (like this) towards cmdlets that already directly write to the information stream (/host), as e.g. `Write-Host -Tee 'Something' would cause redundant (information) output.

    Some of these things are easier to do with the functionality in another command - a bit like NOT having a -RedirectToFile common parameter - that route might be easier than the new common parameter way.

    I presume that for both the "command" and "common parameter" purposes, it will result in a better performance as it doesn't need to invoke the other (Begin, End and Clean) processing blocks. The "command" way will probably more flexible while keeping the performance and the "common parameter" way might result in a nicer syntax on individual cmdlets:

    ... | Tee-Object -Process { Export-Csv -Path .\test.csv  } | ...

    vs.

    ... | Export-Csv -Tee -Path .\test.csv | ...

    (anyways, besides the effort, I see no reason why either of the purposes would exclude each other)

  7. jhoneill commented on Aug 18, 2023

    @jhoneill

    besides the effort, I see no reason why either of the purposes would exclude each other)
    Agree !

    I think if one exists there is a "well there is no need to do the other" argument - since I'm working with a client who won't roll out PowerShell 7 - I'd like to see it as command which can be implemented on Windows PowerShell - my P.o.C should work anywhere, although that would be a community effort. Even for 7 it as a command it could come from the community, where changing the common parameters needs to be a PowerShell team thing.

  8. mklement0 commented on Aug 18, 2023

    @mklement0
    Contributor
    • On a general note: The bar for introducing a new common parameter is high, not least because any new parameter risks name collisions with parameters in existing code (given that there's no "namespace" for common parameters, such as a PS prefix).

    • Tee-Object is the designated cmdlet for "teeing", so enhancing it - only - seems like the appropriate thing to do; by contrast, a -Tee common parameter isn't something that pertains to the cmdlet it is applied to, which is why I think it shouldn't be a common parameter.

      • Leaving that aside, I think that outputting to the information stream is conceptually obscure - and it doesn't work with Format-* cmdlets (the latter only work with direct-to-display output or with Out-Host); e.g., the output from the following is unhelpful:

        Get-Item / | Format-List | Write-Information -InformationAction Continue
        

    Based on the this suggestion for Tee-Object -Process, with a proof-of-concept based on the one mentioned earlier here, the following examples, including the ones in your initial post translate to:

    # "Tee" to the display (host), using default formatting
    # Note: Assumes that -Host is already implemented - see 
    #       https://github.com/PowerShell/PowerShell/issues/19827
    ... | Tee-Object -Host | ...
    
    # "Tee" to the display (host), using alternate formatting
    ... | Tee-Object -Process { Format-List | Out-Host } | ...
    
    # "Tee" to the display (information stream) with arbitrary commands
    ... | Tee-Object -Process   { % { Write-Host "Processing $($_.Name)" } } | ...
    
    # "Tee" to a file (via implicit Out-File) (no display output)
    ... | Tee-Object -Process  { % { $_.Name } }  -FilePath out.txt | ...
    
    # "Tee" to a variable, $out (no display output)
    ... | Tee-Object -Process  { % { $_.Name } }  -Variable out | ...
    
    # "Tee" directly to a command that processes all its pipeline input (no display output)
    ... | Tee-Object -Process  { Export-Csv out.csv }  | ...

    Based on the above, my recommendation is not to implement the proposed common parameter.

  9. iRon7 commented on Aug 19, 2023

    @iRon7
    Author

    Michael Klement (@mklement0),

    Based on the above, my recommendation is not to implement the proposed common parameter.

    You might not be aware, but I (and probably others) see you as a teacher in a lot of things, not just PowerShell but also with items along with the (English) language and general ideas which include the point of view that: one proposal for a specific issue, shouldn't exclude the other.
    For this issue (that originated out of feeling that the -PassThru switch should actually be more common and consistent, which later evolved to this "common -Tee parameter" proposal and the -literally and metaphorically- "forked" Tee-Object -Process command proposal from Dominik Kowalski (@dkaszews)), I guess that the "common parameter" propose might indeed not be a good direction. Not only for your arguments, but due to the effort it requires, the rabbit hole this might lead to and simply because the propose #20123 is a better idea and does supersede this one knowing that it is also much easier to explore, implement and maintain.

    Yet, I would like to keep this issue open for a while to see if anybody else has something to add...

  10. mklement0 commented on Aug 19, 2023

    @mklement0
    Contributor

    Understood, iRon7, and I appreciate the nice feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementNeeds-Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions