Skip to content

Write-Error in a function doesn't set automatic success variable $? to $False (doesn't set the execution status to indicate a non-terminating error) in the caller's scope #3629

Closed

Description

Write-Error is used to signal a non-terminating error, yet $? (the execution-status automatic variable) isn't set to $False in the caller's scope when it is called from an (advanced) function.

By contrast, if you use $PSCmdlet.WriteError from an advanced function, it is.

Steps to reproduce

function foo { Write-Error 'oh no' }; foo; $?
function foo2 { [cmdletbinding()] param() $PSCmdlet.WriteError((New-Object System.Management.Automation.ErrorRecord "oh no", $null, 'NotSpecified', $null)) }; foo2; $?

Expected behavior

# (error details omitted)
$False
# (error details omitted)
$False

Actual behavior

# (error details omitted)
$True
# (error details omitted)
$False

Environment data

PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Darwin Kernel Version 16.5.0: Fri Mar  3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64

Activity

  1. changed the title [-]Write-Error doesn't set $? to $False (doesn't reflect the execution status)[/-] [+]Write-Error doesn't set $? to $False (doesn't set the execution status to indicate a non-terminating error)[/+] on Apr 23, 2017
  2. iSazonov commented on Apr 23, 2017

    @iSazonov
    Collaborator

    function foo { Write-Error 'oh no' }; foo; $?

    As discussed in #3359 here "$?" return status of last statement. So the behavior is by design.

    Michael Klement (@mklement0) Please correct typo in "Steps to reproduce"

  3. added
    WG-Languageparser, language semantics
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    on Apr 23, 2017
  4. changed the title [-]Write-Error doesn't set $? to $False (doesn't set the execution status to indicate a non-terminating error)[/-] [+]Write-Error in a function doesn't set $? to $False (doesn't set the execution status to indicate a non-terminating error) in the caller's scope[/+] on Apr 23, 2017
  5. mklement0 commented on Apr 23, 2017

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov):

    Re as-designed:

    This issue is about the ability for an (advanced) function, as a functional unit, to signal to the caller that a non-terminating error has occurred.

    While you can do that with $PSCmdlet.WriteError(), it is quite cumbersome.

    What I'm suggesting is that Write-Error similarly set $? to $False in the caller's scope on returning from the function. I've updated the title and first post to clarify that.

  6. iSazonov commented on Apr 23, 2017

    @iSazonov
    Collaborator

    Typo:
    function foo { Write-Error 'oh no' }; $? vs function foo { Write-Error 'oh no' }; foo; $?

  7. mklement0 commented on Apr 23, 2017

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov) Ah, got it, thanks - updated.

  8. BrucePay commented on Apr 26, 2017

    @BrucePay
    Collaborator

    Perhaps $? should be all scope.

  9. mklement0 commented on Apr 26, 2017

    @mklement0
    ContributorAuthor

    Bruce Payette (@BrucePay):

    Making the variable -Option AllScope is problematic, because it would restrict use of Write-Error to the very last statement of the function / script (and Write-Error itself would then have to not reflect its success in $?), so that subsequent statements don't overwrite it.

    $PSCmdlet.WriteError() already works as expected:

    • it sets $? in the caller's scope, wherever it is used in the script / function's body

    • while leaving the local copy untouched, so that the remainder of the script / function can continue to make use of it to determine the success of calling other commands.

    I haven't looked at the code yet, but it sounds like it shouldn't be too hard to make Write-Error behave the same way.

  10. 3 remaining items

  11. added
    Committee-ReviewedPS-Committee has reviewed this and made a decision
    and removed
    Review - CommitteeThe PR/Issue needs a review from the PowerShell Committee
    on Jan 8, 2020
  12. SteveL-MSFT commented on Jan 8, 2020

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this. We agree there is a need for a script or pipeline to have a way to set $? but should not change the current behavior of Write-Error which would be a breaking change. Proposals were discussed in #10917

  13. 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.

  14. 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.

  15. 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.

  16. 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

    Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issuesWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions