-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC Proposal: ScriptBlocks to handle non-terminating message processing #196
Comments
What about: & $sb -Id 12345678 -ErrorAction [ActionPreference]::Continue( {Write-MessageLog $_} ) |
That syntax doesn't allow you to determine what you want PowerShell to do with the message based on the logic within the script block (i.e. you can't use |
Why? It is the same but only we put the script block in another ast. I even expect that it will be easier to implement and it will work faster. In addition, it clearly describes the intentions while the original sentence simply offers an arbitrary script block (I mean, if there is to remove the |
@iSazonov There are a number of challenges with the alternate syntax that you proposed, so I'll go into more detail:
For those reasons, I don't think that alternate proposal would be easy to implement at all. |
Moved into PR #219. |
@jpsnover suggested in PowerShell Issue #6010 that an
[-OnError <ScriptBlock>]
is added to the common parameters in PowerShell that takes precedence over-ErrorAction
and$ErrorActionPreference
. In response to that issue, PR #182 has been opened by @TylerLeonhardt with an RFC that proposes we change the trap statement to accommodate non-terminating errors. There are several challenges between the original issue and the proposed RFC:With those challenges in mind, I propose instead that we extend what is allowed in
-*Action
common parameters, such that aScriptBlock
can be passed into those parameters. Further, I also propose that we allow aScriptBlock
to be assigned to any$*Preference
variable as well. This will allow scripters and script, function and module authors to apply custom message processing to their scripts for any type of non-terminating message that is not silenced or ignored.Terminating messages will remain handled by try/catch statements or trap statements the way they are defined in PowerShell 6.2 and earlier releases.
Motivation
As a scripter or a script, function, or module author,
I can use a
ScriptBlock
with*Preference
variables and-*Action
parameters,So that I can perform custom processing for all messages generated by my scripts without the complexity of redirection operators in many different locations.
User experience
Here is an example demonstrating how a scripter may handle non-terminating (as well as terminating) messages in PowerShell once this RFC is implemented:
In the case of the first example, the message log will contain the first verbose message and the warning message, and the internal error message log (that may be from a module) will contain the internal errors that were silenced.
In the case of the second example, the message log will contain both verbose messages.
This approach offers more functionality than the RFC in PR #182 without mixing up the important distinction and decisions that need to made when handing terminating and non-terminating errors.
Specification
If a
ScriptBlock
is present in a$*Preference
variable when a message of the appropriate type is raised, theScriptBlock
would be run with$_
assigned to the appropriateErrorRecord
orInformationalRecord
instance. TheseScriptBlock
instances would be used to process whatever messages they received, and they would identify the action the scripter would like taken once the processing is complete by returning anActionPreference
enumeration value.To make logging messages easier, if the
ScriptBlock
does not return anActionPreference
, PowerShell would automatically apply the defaultActionPreference
for that type of message (Continue
for progress, warning and error messages,SilentlyContinue
for information, verbose and debug messages).While those two paragraphs explain the functionality simply enough, this would probably be a decent amount of work to implement.
It is important to note that this design would not be a breaking change because today you cannot assign a
ScriptBlock
to a-*Action
common parameter, nor can you assign them to a$*Preference
variables.Alternate proposals and considerations
Make the
ScriptBlock
anEventHandler
The
ScriptBlock
implementation looks like event handlers, so an alternative approach would be to define a specific event handler type and having theScriptBlock
design conform to that event handler. For example, in PowerShell we could define aStreamedMessageEventArgs
class that has aAction
property of typeActionPreference
, and require that theScriptBlock
take parameters($MessageRecord, $EventArgs)
, where$MessageRecord
is the message that was raised, and$EventArgs
is an instance ofStreamedMessageEventArgs
used to define theActionPreference
to take once the message is processed. For this approach,$_
would still be assigned to the message record to allow theScriptBlock
logic to remain as simple as possible. Scripters would need to assign a value to$EventArgs.Action
in theScriptBlock
in order to change the default behavior (it would be assigned to the default behavior for the corresponding message type by default).The benefits of this alternative are as follows:
ScriptBlock
returnActionPreference
is a little more explicit (PowerShell will return whatever is output from theScriptBlock
by default, so this makes the important part of what is returned clear).$EventArgs.Action
in theScriptBlock
, keeping their code simple.ScriptBlock
to the parameter or variable they want, and PowerShell will typecast it as an event handler appropriately.The downsides to this approach are as follows:
ScriptBlock
if they want to change the defaultActionPreference
, which may be a little more complicated than simply working with letting anActionPreference
enumeration value (which could even be a string) be returned from theScriptBlock
.Add
-VerboseAction
,-DebugAction
and-ProgressAction
common parametersIt is important to consider the RFC proposal to allow execution preferences to persist beyond module and script scope here because it uses common parameters to pass execution preferences to other modules and/or scripts. In order for that to work properly for all message types, such that
ScriptBlock
action preferences for verbose, debug, and progress messages also propagate beyond module/script scope, we would need to add-VerboseAction
,-DebugAction
, and-ProgressAction
to the common parameter lists. The implementation of these would simply be the same as-WarningAction
or-InformationAction
, but for their respective streams.The benefits of having these additional common parameters are as follows:
ActionPreference
values orScriptBlock
message handlers can propagate beyond the scope of modules or scripts, allowing them to function more like cmdlets do.The downsides to these common parameters are as follows:
-Verbose
and-Debug
common parameters, so there is some overlap; however, the PowerShell engine would raise an error if both-Verbose
and-VerboseAction
were used in an invocation, or-Debug
and-DebugAction
were used in an invocation, so there would be no conflict in invocation. Scripters would simply choose one or the other.The text was updated successfully, but these errors were encountered: