Title: PowerShell class should have an InvokeAndClear() method · Issue #15008 · PowerShell/PowerShell · GitHub
Open Graph Title: PowerShell class should have an InvokeAndClear() method · Issue #15008 · PowerShell/PowerShell
X Title: PowerShell class should have an InvokeAndClear() method · Issue #15008 · PowerShell/PowerShell
Description: Summary of the new feature/enhancement A classic pitfall of the PowerShell class is doing something like this: using (var pwsh = PowerShell.Create()) { DateTime? firstDate = pwsh.AddCommand("Get-Date").Invoke
Open Graph Description: Summary of the new feature/enhancement A classic pitfall of the PowerShell class is doing something like this: using (var pwsh = PowerShell.Create()) { DateTime? firstDate = pwsh.AddCommand("Get-Da...
X Description: Summary of the new feature/enhancement A classic pitfall of the PowerShell class is doing something like this: using (var pwsh = PowerShell.Create()) { DateTime? firstDate = pwsh.AddCommand("G...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/15008
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PowerShell class should have an InvokeAndClear() method","articleBody":"## Summary of the new feature/enhancement\r\n\r\nA classic pitfall of the `PowerShell` class is doing something like this:\r\n\r\n```csharp\r\nusing (var pwsh = PowerShell.Create())\r\n{\r\n DateTime? firstDate = pwsh.AddCommand(\"Get-Date\").Invoke\u003cDateTime?>().FirstOrDefault();\r\n DateTime? secondDate = pwsh.AddCommand(\"Get-Date\").Invoke\u003cDateTime?>().FirstOrDefault();\r\n\r\n Console.WriteLine($\"1: {firstDate}\");\r\n // secondDate is null??\r\n Console.WriteLine($\"2: {secondDate}\");\r\n}\r\n```\r\n\r\nIn this scenario, the PowerShell object's state is affected by the first `AddCommand()` call and we need to clear the commands. This is easily seen here but when `pwsh` is a field being shared between calls it can be subtle.\r\n\r\nIn a lot of my code I end up writing this:\r\n\r\n```csharp\r\nstatic class PowerShellExtensions\r\n{\r\n public static Collection\u003cPSObject> InvokeAndClear(this PowerShell pwsh) => pwsh.InvokeAndClear\u003cPSObject>();\r\n\r\n public static Collection\u003cT> InvokeAndClear\u003cT>(this PowerShell pwsh)\r\n {\r\n try\r\n {\r\n return pwsh.Invoke\u003cT>();\r\n }\r\n finally\r\n {\r\n pwsh.Commands.Clear();\r\n }\r\n }\r\n}\r\n```\r\n\r\nWe should consider adding that to the PowerShell SDK, since I suspect this is a very common use case.","author":{"url":"https://github.com/rjmholt","@type":"Person","name":"rjmholt"},"datePublished":"2021-03-12T20:41:40Z","url":"https://github.com/PowerShell/PowerShell/issues/15008"}
| 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:e52fb8cf-d695-37bf-3ff3-9b8e4515d903 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C492:A965C:4204:5DA5:6AFDD4B0 |
| html-safe-nonce | 815b11b6937bbf796c0f52fb8e6938935007eedb5192169bf957e7378cf55ef2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNDkyOkE5NjVDOjQyMDQ6NURBNTo2QUZERDRCMCIsInZpc2l0b3JfaWQiOiIxNDM3MjA0MzY5NTIxODkwNDgwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 05e9702067d2df592456682ed6b6e0d21ea68f51a32ecf0194b44f95f08b1e9b |
| hovercard-subject-tag | issue:830431311 |
| 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/15008/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ec5836015a452ce19fff1f8d0184c147892d7eaef7877814559fd11f16adf77d/PowerShell/PowerShell/issues/15008 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ec5836015a452ce19fff1f8d0184c147892d7eaef7877814559fd11f16adf77d/PowerShell/PowerShell/issues/15008 |
| og:image:alt | Summary of the new feature/enhancement A classic pitfall of the PowerShell class is doing something like this: using (var pwsh = PowerShell.Create()) { DateTime? firstDate = pwsh.AddCommand("Get-Da... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rjmholt |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9918bd4004419458bcd838065ac2a76b76ebd11705d73909fff5ab99e996fa23 |
| 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 | a231c174493a6a442c0b0544475c918b30128208 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width