Skip to content

PowerShell.EndInvoke(IAsyncResult) doesn't provide a way for users to get the partial results when a terminating error is thrown #8005

Closed

Description

When a terminating error is thrown from the script, PowerShell.EndInvoke(IAsyncResult) will throw the exception and there is no way to get the partial results back (output from before the terminating error).

It would be good to make it able to return the partial results when a terminating error is thrown.

Steps to reproduce

$ps = [powershell]::Create()
$ps.AddScript("'Hello World'; gcm blah -ea stop; 'yeah'") > $null
$a = $ps.BeginInvoke()
$b = $Ps.EndInvoke($a)

Expected behavior

Throw the exception, but at the same time allow the users to get the partial results back.

Actual behavior

Exception is thrown, $b is null, and the partial output is lost.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. daxian-dbw commented on Oct 11, 2018

    @daxian-dbw
    MemberAuthor

    Move Paul Higinbotham (@PaulHigin)'s comment on this issue from #7917 (comment)

    I feel this is fundamentally broken because if a terminating error occurs (including stop) then any data generated up to that point is lost ... a major sin in my book.

    Some ideas to remedy:

    • Catch pipeline stop exception and insert into Error stream as non-terminating error. But this doesn't address a general terminating exception.
    • Catch all terminating exceptions (in EndInvoke) and insert into Error stream, marking the error as terminating in some way.
    • Create a new "EndInvoke" exception class that is thrown, which wraps the actual terminating error and also provides the output buffer as a parameter.
  2. SeeminglyScience commented on Oct 12, 2018

    @SeeminglyScience
    Contributor

    Wouldn't that be expected behavior? A script would work the same way

    function test { 'Hello World'; gcm blah -ea stop; 'yeah' }
    $result = test
    $result
    # no output

    I would think you typically wouldn't want output from a failed invocation, but there's always this overload for when you do

    $ps = [powershell]::Create()
    $ps.AddScript("'Hello World'; gcm blah -ea stop; 'yeah'") > $null
    $inputCollection = [System.Management.Automation.PSDataCollection[psobject]]::new()
    $output = [System.Management.Automation.PSDataCollection[psobject]]::new()
    $a = $ps.BeginInvoke($inputCollection, $output)
    $b = $Ps.EndInvoke($a)
    $output
  3. added
    WG-Engine
    Issue-Discussion
    on Oct 12, 2018
  4. PaulHigin commented on Oct 15, 2018

    @PaulHigin
    Contributor

    -> I would think you typically wouldn't want output from a failed invocation

    It seems to me to be just the opposite. For a long running script I would want to get all output information occurring before a terminating error. It would help understand what happened, what may have caused the error, and what state the machine is in.

  5. SeeminglyScience commented on Oct 15, 2018

    @SeeminglyScience
    Contributor

    For a long running script I would want to get all output information occurring before a terminating error. It would help understand what happened, what may have caused the error, and what state the machine is in.

    I'm inclined to agree that having output information would be useful in the initial stages of development and testing. Once the code is in use I think it would be more common to rethrow or change code paths (and if you do absolutely need it, the other overload is still available to you).

    That said, my main concern isn't really about the output and more about changing the behavior of EndInvoke.

    Since the output collection is stored internally on the PowerShellAsyncResult returned by BeginInvoke, would it make sense to instead have a new method that can retrieve the output? e.g.

    public PSDataCollection<PSObject> GetAsyncOutput(IAsyncResult);
  6. lachlann562 commented on Jan 15, 2019

    @lachlann562

    i agree, this is very important to have a method to get the output so far, likewise there is no way to abort and still capture the output. if i issue EndInvoke it will wait for the job. but if i call stop to terminate it, i have no way to get the output up until that point. there should also be a way to get the breaking exception.

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  9. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  10. added
    Resolution-No Activity
    on Nov 16, 2023
  11. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions