Skip to content

exit causes obscure error in ScriptMethod ETS methods #12235

@mklement0

Description

@mklement0

Note: Calling exit from a function or method is unusual, but it works in functions and in class methods, but not from ScriptMethod methods on custom objects, where it fails with an obscure error.

Steps to reproduce

pwsh -noprofile -c '($obj = [pscustomobject]::new()) | Add-Member -MemberType ScriptMethod Foo { exit 5 }; $obj.Foo()'
$LASTEXITCODE | Should -Be 5

Expected behavior

There should be no output, and the test should succeed.

Actual behavior

An error is reported, and the test fails, because the exit statement wasn't effective (the exit code was implicitly set by the unexpected statement-terminating error, to 1):

MethodInvocationException: Exception calling "Foo" with "0" argument(s): "System error."
InvalidResult:
Line |
   1 | $LASTEXITCODE | Should -Be 5
     | Expected 5, but got 1.

Environment data

PowerShell Core 7.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions