Title: Select-Object -First X skips End{ } blocks of previous cmdlets · Issue #7930 · PowerShell/PowerShell · GitHub
Open Graph Title: Select-Object -First X skips End{ } blocks of previous cmdlets · Issue #7930 · PowerShell/PowerShell
X Title: Select-Object -First X skips End{ } blocks of previous cmdlets · Issue #7930 · PowerShell/PowerShell
Description: Using the -First $n parameter for Select-Object completely skips the end {} blocks of all cmdlets preceding it in the pipeline. This can be quite hazardous, e.g., for a cmdlet that access a resource that needs to have that handle properl...
Open Graph Description: Using the -First $n parameter for Select-Object completely skips the end {} blocks of all cmdlets preceding it in the pipeline. This can be quite hazardous, e.g., for a cmdlet that access a resourc...
X Description: Using the -First $n parameter for Select-Object completely skips the end {} blocks of all cmdlets preceding it in the pipeline. This can be quite hazardous, e.g., for a cmdlet that access a resourc...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/7930
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Select-Object -First X skips End{ } blocks of previous cmdlets","articleBody":"Using the `-First $n` parameter for `Select-Object` *completely skips* the `end {}` blocks of **all** cmdlets preceding it in the pipeline.\r\n\r\nThis can be quite hazardous, e.g., for a cmdlet that access a resource that needs to have that handle properly disposed of (like a file) may not release that handle correctly.\r\n\r\nThere is no `finally {}` block available to PS, and the method in which `Select-Object` terminates the pipeline appears to happen in such a manner that *no script-based function can account for*; that is, even *with* a `try...finally` construct in the cmdlets' `process {}` blocks it will not kick off in the event that `Select-Object` is used to prematurely end the pipeline.\r\n\r\nInstead, a method should be exposed for `Select-Object` (and potentially other cmdlets?) to *safely* terminate the pipeline by ceasing all `process` steps and kicking off the chain of `end {}` steps in the pipeline.\r\n\r\nSteps to reproduce\r\n------------------\r\n(shamelessly pinched from @TimCurwick's [fantastic example](https://gist.github.com/TimCurwick/c1794b1c2f978fc5fd9c86d19a683636) he posted in the PS Slack)\r\n```powershell\r\nfunction test {\r\n [cmdletbinding()]\r\n param(\r\n [Parameter(ValueFromPipeline)]\r\n $X \r\n )\r\n begin {\r\n Write-Host \"Begin test\"\r\n }\r\n process {\r\n Write-Host \"Process test\"\r\n $X\r\n }\r\n end {\r\n Write-Host \"End test\"\r\n }\r\n}\r\n \r\n1, 2, 3 | test | test | select -first 2\r\n```\r\n\r\nExpected behavior\r\n-----------------\r\n\r\n```none\r\nBegin Test\r\nBegin Test\r\nProcess Test\r\nProcess Test\r\n1\r\nProcess Test\r\nProcess Test\r\n2\r\nEnd Test\r\nEnd Test\r\n```\r\n\r\nActual behavior\r\n---------------\r\n\r\n```none\r\nBegin test\r\nBegin test\r\nProcess test\r\nProcess test\r\n1\r\nProcess test\r\nProcess test\r\n2\r\n```\r\n\r\nEnvironment data\r\n----------------\r\n\r\n\u003c!-- provide the output of $PSVersionTable --\u003e\r\n\r\n```powershell\r\n\u003e $PSVersionTable\r\n\r\nName Value\r\n---- -----\r\nPSVersion 6.1.0\r\nPSEdition Core\r\nGitCommitId 6.1.0\r\nOS Microsoft Windows 10.0.17763\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n```\r\n","author":{"url":"https://github.com/vexx32","@type":"Person","name":"vexx32"},"datePublished":"2018-10-03T01:36:08.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/7930/PowerShell/issues/7930"}
| 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:e928a8dd-388b-8607-63cc-e27d5c0eb820 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1AA:2DEFFD:4349CD:5B46A3:6A5A7334 |
| html-safe-nonce | f68828d87fbab633c36344bc6cacd062fb3ddaafd5ed6170cf39960c5a191ce6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUFBOjJERUZGRDo0MzQ5Q0Q6NUI0NkEzOjZBNUE3MzM0IiwidmlzaXRvcl9pZCI6IjYzMjQ1NDEwMTEwMzg3MjkwMTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7e9b563f45ee644c70ffd7586f553020a709057b51797afb726b42030dacf29b |
| hovercard-subject-tag | issue:366141688 |
| 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/7930/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e30a9b957dd8497b72ef85ce673c785c74f3b95cd914d1bacfe514fb4db83bc6/PowerShell/PowerShell/issues/7930 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e30a9b957dd8497b72ef85ce673c785c74f3b95cd914d1bacfe514fb4db83bc6/PowerShell/PowerShell/issues/7930 |
| og:image:alt | Using the -First $n parameter for Select-Object completely skips the end {} blocks of all cmdlets preceding it in the pipeline. This can be quite hazardous, e.g., for a cmdlet that access a resourc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vexx32 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 922bdba685ea48400234d557977f0e32d9adbae6f5125725b4f48cacc6801106 |
| 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 | 0437efb828a4041956ce45d1f7fe69fff38926c7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width