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.

b014cd01-55c4-432f-8a80-aee721e9c416

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.

  1. Bind the named parameters.

  2. Bind the positional parameters.

  3. Bind the common parameters.

  4. Bind the parameters to support calls to the ShouldProcess method.

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

  1. Bind command-defined pipeline parameters.

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

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

  1. Determine whether all the mandatory parameter values are available, and fail the pipeline if they are not.

  2. Determine whether a single parameter set is defined, and fail the pipeline if it is not.

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

  4. When the pipeline processor returns from the ProcessRecord method, set any pipeline parameters to their original values.

  5. Check for more pipeline objects.

  6. If there are more objects, bind the pipeline parameters again.

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

See Also

Community Additions

Show:
© 2014 Microsoft