Title: Tee-Object -Process · Issue #20123 · PowerShell/PowerShell · GitHub
Open Graph Title: Tee-Object -Process · Issue #20123 · PowerShell/PowerShell
X Title: Tee-Object -Process · Issue #20123 · PowerShell/PowerShell
Description: Summary of the new feature / enhancement Tee-Object could use a new parameter Process, accepting a scriptblock which will be invoked for every item going through it. Example usecases: #19827 - ... | Tee-Object -Host | ... could be writte...
Open Graph Description: Summary of the new feature / enhancement Tee-Object could use a new parameter Process, accepting a scriptblock which will be invoked for every item going through it. Example usecases: #19827 - ... ...
X Description: Summary of the new feature / enhancement Tee-Object could use a new parameter Process, accepting a scriptblock which will be invoked for every item going through it. Example usecases: #19827 - ... ...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/20123
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Tee-Object -Process","articleBody":"### Summary of the new feature / enhancement\r\n\r\n`Tee-Object` could use a new parameter `Process`, accepting a scriptblock which will be invoked for every item going through it. Example usecases:\r\n\r\n* #19827 - `... | Tee-Object -Host | ...` could be written as `... | Tee-Object -Process { Write-Host } | ...` (bit verbose, so #19827 on its own is probably still a good idea)\r\n* #19989 - `... | ExportCsv files.csv -PassThru | ...` could be written as `... | Tee-Object -Process { Export-Csv files.csv } | ...` (saves on adding `-PassThru` to each missing cmdlet\r\n* #20121 - `... | Write-Host -PassThru -Format \"Processing: $_\" | ...` could be written as `... | Tee-Object -Process { Write-Host \"Processing: $_\" }`\r\n\r\n### Proposed technical implementation details (optional)\r\n\r\nOpens:\r\n1. Is there some magic we use to split and connect pipes, so that it correctly calls `Begin`, `Process`, `End`? Without it, we may either accidentally call the cmdlet for each object separately, causing each of them to clobber the last. This means we cannot simply implement it as `ForEach-Item { $_ | $Process; $_ }`:\r\n\t```pwsh\r\n\t> 0..4 | ConvertTo-Json\r\n\t[\r\n\t\t0,\r\n\t\t1,\r\n\t\t2,\r\n\t\t3,\r\n\t\t4\r\n\t]\r\n\t> 0..4 | %{ ConvertTo-Json }\r\n\t0\r\n\t1\r\n\t2\r\n\t3\r\n\t4\r\n\t```\r\n1. Similarly, if we first bulk all of the piped items and only later push them into the secondary pipe, this will break streaming, as with implementation with: `ForEach-Object -Begin { $pipe = [List[object]]:new() } -Process { $pipe.Add($_); $_ } -End { $pipe | $Process }`. It will be especially visible with usecase 1.: `Tee-Object -Process { Write-Host }` will only start outputting once the main pipeline has finished.\r\n1. How should we treat implicit or explicit output from the secondary pipe? Should it affect the primary pipe, should it be written to host, or should it be ignored? For `ForEach-Object`, the first is true, as can be seen with `0..4 | %{ $_; $_ } | Measure-Object` outputting `10`.\r\n1. If we can split the pipeline properly, do we have to worry about one part of it mutating the pipe for the other? In other words, what should be the order of execution of `Process`? Secondary pipe then primary, other way around, concurrent, undefined?\r\n\t```pwsh\r\n\tGet-ChildItem | Tee-Object -Process { $_.Length = 123 } | ConvertTo-Json > files.json\r\n\t# Should the resulting JSON contain original Length, or all 123, as if with `ForEach-Object`?\r\n\tGet-ChildItem | Tee-Object -Process { ConvertTo-Json > files.json } | %{ $_.Length = 123 }\r\n\t# What if we switch main and secondary pipes around?\r\n\t```\r\n1. Is the syntax I have been showing with scriptblock omitting `$_` and pretending all the items are piped from the left even legal or reasonable? I don't see how to express some of the intentions I have shown without it, unless we make `$_` in `Tee-Object -Process { $_ | Export-Csv files.csv }` mean the entire pipeline. Otherwise, maybe some new symbol like `$%` would make sense, as `%` is commonly already associated with item processing as default alias for `ForEach-Object`.","author":{"url":"https://github.com/dkaszews","@type":"Person","name":"dkaszews"},"datePublished":"2023-08-16T18:13:53Z","url":"https://github.com/PowerShell/PowerShell/issues/20123"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a96edbc2-648a-7f91-94dc-611f95d01161 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DAF8:32893B:6CDF0:941B5:6AFD5948 |
| html-safe-nonce | f4cb30d3965e78a59bd7ff7beff26f2708493bb8b97336d9254a1f6bf6616644 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQUY4OjMyODkzQjo2Q0RGMDo5NDFCNTo2QUZENTk0OCIsInZpc2l0b3JfaWQiOiIyOTQ5MjYwMjg0ODIyMTE2NjgwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 824f341c6ce3f16cf73b29abf64e7679142dd0d073682e588105e23b40c4475c |
| hovercard-subject-tag | issue:1853700499 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/20123/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d7c1808a03bd7e90bdf477f4347f3be35866244e164fbb8d499f7bc46b00c4f9/PowerShell/PowerShell/issues/20123 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d7c1808a03bd7e90bdf477f4347f3be35866244e164fbb8d499f7bc46b00c4f9/PowerShell/PowerShell/issues/20123 |
| og:image:alt | Summary of the new feature / enhancement Tee-Object could use a new parameter Process, accepting a scriptblock which will be invoked for every item going through it. Example usecases: #19827 - ... ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dkaszews |
| hostname | github.com |
| expected-hostname | github.com |
| None | 66653c2e55a6fff2930be1bdf07ee4f8aa137e3956d056c822563be6f878755e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b6f01f7cb9f35f8c09949773241a3d49dd04b340 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width