You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PowerShell class should have an InvokeAndClear() method #15008
A classic pitfall of the PowerShell class is doing something like this:
using(varpwsh=PowerShell.Create()){DateTime?firstDate=pwsh.AddCommand("Get-Date").Invoke<DateTime?>().FirstOrDefault();DateTime?secondDate=pwsh.AddCommand("Get-Date").Invoke<DateTime?>().FirstOrDefault();Console.WriteLine($"1: {firstDate}");// secondDate is null??Console.WriteLine($"2: {secondDate}");}
In 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.
Just to spell it out, because the fact that your sample code actually does work confused me:
The commands accumulate in the pwsh.Commands collection, and subsequent .Invoke() calls try to execute them all, in addition to accumulating stream output in pwsh.Streams across calls, as Patrick Meinecke (@SeeminglyScience) points out.
Your second .Invoke() call is therefore roughly equivalent to:
and the latter just happens to work, because Get-Date also accepts a [datetime] instance via the pipeline, so this pipeline is in effect the same as just calling Get-Date alone.
Adding a -Date argument to the 2nd GetDate call would surface the problem, however, because something like Get-Date | Get-Date 1970-01-01 indeed fails.
As for the API surface and your extension method:
If the intent is to also clear the streams, you cannot include .Streams.ClearStreams() in the .InvokeAndClear() method, as you would instantly wipe out the .Invoke() call's non-success stream output.
So I wonder if something along the following lines is called for instead:
A fluent .Reset() method that calls .Commands.Clear() and .Streams.ClearStreams() and returns the PowerShell instance itself, as the existing methods do.
This means that resetting can then either be performed stand-alone:
pwsh.Reset();// same as: .Commands.Clear(); .Streams.ClearStreams(); return this;
Or, fluently, as part of recurring invocations on a reusable PowerShell instance:
I would definitely love to see a PowerShell.Reset() method as proposed by Michael Klement (@mklement0)!
I've found myself doing pwsh.Commands.Clear(); pwsh.Streams.ClearStreams(); too (and once discovered the hard way that I had to).
Reacted by Michael Klement and Rain Sallow (/u/ta11ow)
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
Activity
SeeminglyScience commentedon Mar 12, 2021
Also the error, verbose, etc streams
mklement0 commentedon Mar 13, 2021
Just to spell it out, because the fact that your sample code actually does work confused me:
The commands accumulate in the
pwsh.Commandscollection, and subsequent.Invoke()calls try to execute them all, in addition to accumulating stream output inpwsh.Streamsacross calls, as Patrick Meinecke (@SeeminglyScience) points out.Your second
.Invoke()call is therefore roughly equivalent to:which in turn is equivalent to:
and the latter just happens to work, because
Get-Datealso accepts a[datetime]instance via the pipeline, so this pipeline is in effect the same as just callingGet-Datealone.Adding a
-Dateargument to the 2ndGetDatecall would surface the problem, however, because something likeGet-Date | Get-Date 1970-01-01indeed fails.As for the API surface and your extension method:
If the intent is to also clear the streams, you cannot include
.Streams.ClearStreams()in the.InvokeAndClear()method, as you would instantly wipe out the.Invoke()call's non-success stream output.So I wonder if something along the following lines is called for instead:
A fluent
.Reset()method that calls.Commands.Clear()and.Streams.ClearStreams()and returns the PowerShell instance itself, as the existing methods do.This means that resetting can then either be performed stand-alone:
Or, fluently, as part of recurring invocations on a reusable
PowerShellinstance:Fs00 commentedon Mar 15, 2021
I would definitely love to see a
PowerShell.Reset()method as proposed by Michael Klement (@mklement0)!I've found myself doing
pwsh.Commands.Clear(); pwsh.Streams.ClearStreams();too (and once discovered the hard way that I had to).rjmholt commentedon Mar 26, 2021
Quick thought since I have the issue open:
I suggest that because in various scenarios, I don't want to clear the streams.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.