Repository navigation
Description
Summary of the new feature / enhancement
Why adding the -PassThru switch on a case-by-case bases like: #13713 Add -PassThru parameter to Set-Clipboard?
In my believe every (advanced) function/cmdlet that has a ValueFromPipeline input and no (success stream) pipeline output, should have (an easy standard way to implement)¹ a default -PassThru functionality.
-
- any function/cmdlet that doesn't have an explicit custom
-PassThruparameter defined, and/or - based on an explicit empty output definition
[OutputType([System.Void])]
(but I fear that theOutputTypeproperty isn't correctly inplemented in every cmdlet. Anyways a -redundant- common-PassThruwon't harm anything)
- any function/cmdlet that doesn't have an explicit custom
Take e.g.: Powershell - Get IP Addresses for hostnames from File (more than 150K hostnames)
The Export-Csv appears not to support the -PassThru parameter:
Export-Csv: A parameter cannot be found that matches parameter name 'PassThru'.
But I see no reason why these cmdlets shouldn't have a -PassThru parameter.
Wishful thinking:
... | Export-Csv .\test.csv -PassThru | Format-TableThis purpose might avoid wrapping cmdlet pipelines
This will affect functions and cmdlets along with:
Get-command | Where-Object { $_.Parameters } | Where-Object {
$_.Parameters.getEnumerator().Foreach{ $_ }.Value.Attributes.ValueFromPipeline -eq $True -and
!$_.OutputType -and !($_.Parameters.get_Keys() -eq 'PassThru')
}A automated -PassThru parameter might even be included in some very common cmdlets which basically makes a cmdlet as Tee-Object redundant:
Tee-Object |
-PassThru |
comment |
|---|---|---|
Tee-Object -FilePath .\Test.log |
Out-File -PassThru -FilePath .\Test.log |
|
Tee-Object -Variable MyVar |
Set-Variable -PassThru -Name MyVar |
already exists |
(Tee-Object -Host) |
Out-Host -PassThru |
See: #19827 |
Activity
[-]Standard `-PassThru` functionality for every (advanced) function / cmdlet.[/-][+]`-PassThru` as comon parameter[/+]jhoneill commentedon Jul 17, 2023
I think that is a pretty good design goal - but it is really the job of the command author to make it happen, which is why we have a piecemeal "add it here" , "add it there" . A task to find all the places it should be present and adding it where it is missing would seem like a smart thing...
Adding -PassThru logic as part of common parameters may cause more problems than it solves (for example I was working with a pipeline of 200,000 objects today. Nothing is output in the process block but things are output in the end block. Anything which checks for output would need to buffer everything and then output it at after the end / clean).
iRon7 commentedon Jul 18, 2023
James O'Neill (@jhoneill),
That will certainly add some value, but I think it shouldn't just concern native PowerShell cmdlets but also custom Advanced Functions created in the field.
Even without any (output-check) logic, using the
-PassThruswitch will just be something you shouldn't use on a script that already outputs to the success stream (likeConvertTo-Csv -PassThru, as it will mix up the passed through input with the actual output) but it won't break anything existing.But I agree; it might lead to confusion if some one tries to actually do this on a function/cmdlet that already output to the success stream. So, what about scripts that have explicitly set the
OutputTypetoSystem.Void? Like:Meaning, all of these conditions should be true:
-PassThruparameter defined by itselfValueFromPipelineparameterOutputTypedefined that is set toVoid($_.OutputType.Name -eq 'Void')[-]`-PassThru` as comon parameter[/-][+]`-PassThru` as common parameter[/+]jhoneill commentedon Jul 19, 2023
I think (and it is only one view). OutputType is widely left unset, and only very rarely set to Void. Yes we could save authors a little effort and enable passthru for those functions which do.
I think the objection to doing it comes from command authors... take a version of the SO article.
I have a
Remove-Thingcommand, so I can doGet-Thing | where-object ... | Remove-Thingas author it never crossed my mind that someone would want to see what was removed and have... | Remove-Thing -passthru | Ft. Enabling this would help users of my command IF they only do things which make sense with things that have been removed.And then we get the argument "I don't want users to be able to do something stupid" vs "Don't stop me doing something because someone else might be stupid" .
The user of my command can do
Get-Thing | where-object -outvariable Things ... | Remove-Thingand do$things | ftas its own command so I can't stop them doing something stupid, I just make them do it asynchronously. And if where is not selecting the right things they don't see that until remove has tried to delete them.So the user needs to do
Get-Thing | where-object -outvariable Things ... | ft ; $things | remove-thingIt would be better for them if they could just doGet-Thing | where-object -outvariable Things ... | Out-host -passthru | Remove-Thing, but of course out-host doesn't support passthru.The other issue is if we look at -Passthru in (for example) add-Member it doesn't matter if we do
$things | Add-Member -passthruor
Add-Member -in $things | -passthruI think the common parameter route only works for the first one.
There's still a good idea here, even if we only get to adding something to best practices guide to say "support -Passthru" - but maybe others have ideas for how the original idea can be made to work nicely.
iRon7 commentedon Jul 20, 2023
James O'Neill (@jhoneill),
I have done an additional Feature Request / Idea for this: #20001 Add
Write-*proxy for eachFormat-*cmdletI think, the
-PassThruparameter should push for a standard (consistent) expectation even it is not always the desired implementation (e.g. in the case of aRemove-Thingfunction which its own-Filterparameter, it should implement its own-PassRemovedor even-PassLeftoverparameters possibly aside from a standard-PassThru)I guess the standard should be something like this:
Where any (unintended) deviations from the standard
PassThruimplementation should be prevented.(but not written in stone, if the author wants to do his own
PassThruimplementation, he can do so by adding a normal "custom"$PassThruparameter)Meaning, avoid:
processblock(This probably also involves the different input categories mentioned in Consistently document a scalar -InputObject parameter as an implementation detail or make item-by-item processing cmdlets explicitly iterate over collections #4242)
if ($PassThru) { "$_" }(even the$InputObjectis a string)Knowing that
$_could be a reference to an object that might have be altered by the cmdlet(not sure about this. This is probably the
Get-Memberthing you mentioned)-PassThruoutputAgree.
Btw, I renounced from the
[OutputType([System.Void])]trigger idea as it contradicts itself (when set, it actually allows for a -passthru- output 👎), but still think that a "trigger switch" for a common/automated standard-PassThruimplementation could be welcome. (maybe a[CmdletBinding(SupportsPassThru)]).Whatever decision is made for this request, I will push for documentation afterwards as e.g. a section in about Functions Advanced Parameters document.
dkaszews commentedon Jul 21, 2023
How about
... | Tee-Object -Process { Export-Csv .\test.csv } | Format-Table?It would have similar semantics to
ForEach-Object { $_ | $action; $_ }, but act on the entire pipe once it ends, instead of individual objects. In fact, it could probably be implemented with something likeForEach-Object -Begin { $pipe = [List[object]]:new() } -Process { $pipe.Add($_); $_ } -End { $pipe | $action }, save for scoping and maybe outputting where it should not.This kind of solution would be way more generic and more closely match how Linux uses
teeand process substitution.iRon7 commentedon Jul 22, 2023
Dominik Kowalski (@dkaszews),
I like the generic
Tee-Object -Processidea and would suggest to write a seperate purpose for that as this doesn't exclude the initial-PassThrupurpose which -I think- is easier to understand for a average PowerShell user.dkaszews commentedon Jul 22, 2023
iRon7
You mean separate proposal/issue/feature request?
mklement0 commentedon Aug 16, 2023
iRon7, re:
Note that the cmdlets that currently implement their own
-PassThruswitch mostly use semantics that differ from your proposal:A few examples:
Import-Module,Set-Variable,Expand-Archive,Rename-Item,Start-Process: with-PassThru, they emit the resulting module, variable object, file-system-info objects, process object.The only exception I'm aware of is
Set-Content, which truly passes its input through. (WhileCompare-Objectpasses input through too, it only does so selectively, and with ETS decorations.)dkaszews commentedon Aug 16, 2023
I have written up a proposal, but in the process (pun maybe intended) came up with so many opens, that sprinkling
-PassThruwhere applicable may be the better way to go.mklement0 commentedon Aug 17, 2023
Thanks for writing up a new proposal, Dominik Kowalski (@dkaszews) - indeed, there are challenging conceptual issues to work out, but I don't think that
-PassThruas a common parameter that the the infrastructure handles automatically can ever work, as implied by my previous comment: the predominant semantics are to pass through (out) whatever the cmdlet does with the input, not the input.iRon7 commentedon Aug 17, 2023
Michael Klement (@mklement0),
Good point, to be honest, I was aware but didn't completely thinking this through assuming that there would be a few exceptions to what you would expect¹ and not that there would be just one "consistent"
Set-Content.You're right, a common
-PassThruwith a consistent semantics would break the current individual implementations.Therefore I will close this issue and open a new issue:
#20133 A common
-Reprocessparameter for passing through the current input item.-Passthruonly includes a verb, I would expect that it passes the current item ($_) as that is common for all concerned cmdlets