Skip to content

Using the exit statement from an advanced function logs TerminatingError(): "System error." within a transcription. #26625

@mjr4077au

Description

@mjr4077au

Prerequisites

Steps to reproduce

This was discussed on the PowerShell Discord server with @SeeminglyScience earlier today. When exit is used within an advanced function, the underlying exception message from System.Management.Automation.ExitException (System.SystemException) is written erroneously to the transcription. This causes no issue and has been there since at least Windows PowerShell 5,1, but can be confusing to users.

To reproduce:

  1. Enable global transcription via ADMX or registry.
  2. Save the following code to disk:
function Exit-Script
{
    [CmdletBinding()]
    param
    (
    )
    exit 255
}
Exit-Script
  1. Run pwsh.exe -File C:\Path\To\Saved\Script.ps1.
  2. Review transcripion and observe the following:
**********************
PowerShell transcript start
Start time: 20251217114847
...
SerializationVersion: 1.1.0.1
WSManStackVersion: 3.0
**********************
PS>CommandInvocation(Script.ps1): "Script.ps1"
>> TerminatingError(): "System error."
**********************
PowerShell transcript end
End time: 20251217114847
**********************

Expected behavior

That this not occur.

Actual behavior

**********************
PowerShell transcript start
Start time: 20251217114847
...
SerializationVersion: 1.1.0.1
WSManStackVersion: 3.0
**********************
PS>CommandInvocation(Script.ps1): "Script.ps1"
>> TerminatingError(): "System error."
**********************
PowerShell transcript end
End time: 20251217114847
**********************

Error details

Below is the stack trace I received in Visual Studio while I set a breakpoint where such a line is written:

>	System.Management.Automation.dll!System.Management.Automation.Internal.PipelineProcessor.LogExecutionException(System.Exception exception) Line 176	C#
 	System.Management.Automation.dll!System.Management.Automation.Internal.PipelineProcessor.GetFirstError(System.Management.Automation.RuntimeException e) Line 597	C#
 	System.Management.Automation.dll!System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(object input) Line 574	C#
 	System.Management.Automation.dll!System.Management.Automation.PipelineOps.InvokePipeline(object input, bool ignoreInput, System.Management.Automation.CommandParameterInternal[][] pipeElements, System.Management.Automation.Language.CommandBaseAst[] pipeElementAsts, System.Management.Automation.CommandRedirection[][] commandRedirections, System.Management.Automation.Language.FunctionContext funcContext) Line 524	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.ActionCallInstruction<object, bool, System.Management.Automation.CommandParameterInternal[][], System.Management.Automation.Language.CommandBaseAst[], System.Management.Automation.CommandRedirection[][], System.Management.Automation.Language.FunctionContext>.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 608	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 355	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 355	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 355	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.Interpreter.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 105	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.LightLambda.RunVoid1<System.Management.Automation.Language.FunctionContext>(System.Management.Automation.Language.FunctionContext arg0) Line 81	C#
 	System.Management.Automation.dll!System.Management.Automation.PSScriptCmdlet.RunClause(System.Action<System.Management.Automation.Language.FunctionContext> clause, object dollarUnderbar, object inputToProcess) Line 2368	C#
 	System.Management.Automation.dll!System.Management.Automation.PSScriptCmdlet.DoProcessRecord() Line 2259	C#
 	System.Management.Automation.dll!System.Management.Automation.CommandProcessor.ProcessRecord() Line 384	C#
 	System.Management.Automation.dll!System.Management.Automation.CommandProcessorBase.DoExecute() Line 575	C#
 	System.Management.Automation.dll!System.Management.Automation.Internal.PipelineProcessor.Inject(object input, bool enumerate) Line 1252	C#
 	System.Management.Automation.dll!System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(object input) Line 523	C#
 	System.Management.Automation.dll!System.Management.Automation.PipelineOps.InvokePipeline(object input, bool ignoreInput, System.Management.Automation.CommandParameterInternal[][] pipeElements, System.Management.Automation.Language.CommandBaseAst[] pipeElementAsts, System.Management.Automation.CommandRedirection[][] commandRedirections, System.Management.Automation.Language.FunctionContext funcContext) Line 524	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.ActionCallInstruction<object, bool, System.Management.Automation.CommandParameterInternal[][], System.Management.Automation.Language.CommandBaseAst[], System.Management.Automation.CommandRedirection[][], System.Management.Automation.Language.FunctionContext>.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 608	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 355	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 355	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 355	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 355	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.Interpreter.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) Line 105	C#
 	System.Management.Automation.dll!System.Management.Automation.Interpreter.LightLambda.RunVoid1<System.Management.Automation.Language.FunctionContext>(System.Management.Automation.Language.FunctionContext arg0) Line 81	C#
 	System.Management.Automation.dll!System.Management.Automation.PSScriptCmdlet.RunClause(System.Action<System.Management.Automation.Language.FunctionContext> clause, object dollarUnderbar, object inputToProcess) Line 2368	C#
 	System.Management.Automation.dll!System.Management.Automation.PSScriptCmdlet.DoEndProcessing() Line 2276	C#
 	System.Management.Automation.dll!System.Management.Automation.CommandProcessorBase.Complete() Line 601	C#
 	System.Management.Automation.dll!System.Management.Automation.CommandProcessorBase.DoComplete() Line 642	C#
 	System.Management.Automation.dll!System.Management.Automation.Internal.PipelineProcessor.DoCompleteCore(System.Management.Automation.CommandProcessorBase commandRequestingUpstreamCommandsToStop) Line 653	C#
 	System.Management.Automation.dll!System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(object input) Line 538	C#
 	System.Management.Automation.dll!System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper() Line 400	C#
 	System.Management.Automation.dll!System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc() Line 588	C#
 	System.Management.Automation.dll!System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProcImpersonate() Line 544	C#
 	System.Management.Automation.dll!System.Management.Automation.Runspaces.PipelineThread.WorkerProc() Line 1217	C#
 	[External Code]

Environment data

PS C:\Repos\PSAppDeployToolkit\src> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.6.0-preview.99
PSEdition                      Core
GitCommitId                    7.6.0-preview.99
OS                             Microsoft Windows 10.0.26100
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.4
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS C:\Repos\PSAppDeployToolkit\src>

Visuals

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.WG-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