Cmdlet Processing Lifecycle
The following illustration shows how Windows PowerShell processes a command after the pipeline is invoked. This flow includes an initial binding phase for command-line parameters, the beginning of the command processing, a second binding phase for pipeline parameters, the processing of the records, and the end of the command process.
Binding Parameters that Take Command-Line Input
During its first binding phase, the Windows PowerShell runtime uses its pipeline processor to bind the arguments to the parameters. The arguments can be interactively specified by the user as command-line input, or they can be programmatically specified by the host application. The pipeline processor triggers the command processor for each of the affected commands to perform the initial binding. For each command, the first binding phase is performed in the following order.
Bind the named parameters.
Bind the positional parameters.
Bind the common parameters.
Bind the parameters to support calls to the ShouldProcess method.
Bind the named dynamic parameters
Bind the positional dynamic parameters.
During binding, the pipeline processor uses the metadata for the parameters, the type definitions of the extended type system (ETS), and the type coercion of parameter values. Type coercion is the process of making a value of a particular .NET Framework type become a parameter value of another type.
If the pipeline processor finishes its processing and finds any unbound parameters for the commands that the pipeline is receiving as command-line input, the pipeline fails, and processing ceases. If all the parameters for these commands are bound successfully, the pipeline processor starts processing the records.
Starting Command Processing
The pipeline processor starts command processing after all the parameters that take command line input are bound to their values. To do this, the processor calls the BeginProcessing method for the first command that is sent to the pipeline. This method does nothing if the command uses the default implementation. However, if the command implements an override for the BeginProcessing method, the override of the method performs any preprocessing and initialization that is required by the command.
When the pipeline processor returns from the first call to the BeginProcessing method, the pipeline processor calls the method in the rest of the commands in the pipeline until all the commands have been called. If a command appears in the pipeline more than one time, each occurrence is treated as a separate command.
Binding Parameters That Take Pipeline Input
After all the commands are processed, the pipeline processor now goes through another binding phase. In this operation, the processor binds the values to the parameters for each command that accepts pipeline input. For each command that accepts pipeline input, the processor binds values to the parameters in the following order.
Bind command-defined pipeline parameters.
Bind dynamic pipeline parameters.
If the pipeline processor finds any unbound parameters for the commands that accept pipeline input, the pipeline fails, and processing ceases. If all the parameters for these commands are bound successfully, the pipeline processor starts to process records.
The pipeline processor starts processing records after all the parameters that accept pipeline line input are bound to their values. The pipeline processor starts with the first command in the pipeline. The pipeline processor follows these record processing steps.
Determine whether all the mandatory parameter values are available, and fail the pipeline if they are not.
Determine whether a single parameter set is defined, and fail the pipeline if it is not.
Call the ProcessRecord method in the downstream command. This method does nothing if the command uses the default implementation. However, if the command implements an override for the ProcessRecord method, the method processes the record.
When the pipeline processor returns from the ProcessRecord method, set any pipeline parameters to their original values.
Check for more pipeline objects.
If there are more objects, bind the pipeline parameters again.
Repeat these steps until all the records for the commands in the pipeline have been processed.
After the records for all the commands in the pipeline have been processed, the runspace then ends its command processing.
Ending the Command Process
After all the records have been processed, the pipeline processor tells each successive command processor to call the EndProcessing method in the associated command. This method does nothing if the command uses the default implementation. However, if the command implements an override for the EndProcessing method, the override performs any postprocessing required by the command.