about_functions_advanced_methods

TOPIC
    about_functions_advanced_methods

SHORT DESCRIPTION
    Describes how Functions that specify the CmdletBinding attribute can use
    the methods and properties that are available to compiled cmdlets.

LONG DESCRIPTION
    Functions that specify the CmdletBinding attribute can access a number of
    methods and properties through the $pscmdlet Variable. These methods
    include the following methods:

        – Input-processing methods that compiled cmdlets use to do their work.

        – The ShouldProcess and ShouldContinue methods that are used to get
         user feedback before an action is performed.

        – The ThrowTerminatingError method for generating error records.

        – Several Write methods that return different types of output.

        – Several Write methods that return different types of output.

    All the methods and properties of the PSCmdlet class are available to
    advanced Functions. For more information about these methods and
    properties, see System.Management.Automation.PSCmdlet in the MSDN
    (Microsoft Developer Network) library at
    http://go.microsoft.com/fwlink/?LinkId=142139.

Input Processing Methods

     The methods described in this section are referred to as the input
     processing methods. For Functions, these three methods are represented
     by the Begin, Process, and End blocks of the Function. Each Function
     must include one or more of these blocks. The Windows PowerShell runtime
     uses the code within these blocks when it is running a Function. (These
     blocks are also available to Functions that do not use the CmdletBinding
     attribute.)

    Begin
     This block is used to provide optional one-time preprocessing for the
     Function. The Windows PowerShell runtime uses the code in this block one
     time for each instance of the Function in the pipeline.

    Process
     This block is used to provide record-by-record processing for the
     Function. This block might be used any number of times, or not at all,
     depending on the input to the Function. For example, if the Function is
     the first command in the pipeline, the Process block will be used one
     time. If the Function is not the first command in the pipeline, the
     Process block is used one time for every input that the Function
     receives from the pipeline. If there is no pipeline input, the Process
     block is not used.

     This block must be defined if a Function parameter is set to accept
     pipeline input. If this block is not defined and the parameter accepts
     input from the pipeline, the Function will miss the values that are
     passed to the Function through the pipeline.

     Also, when the Function supports confirmation requests (when the
     SupportsShouldProcess parameter of the Parameter attribute is set to
     $True), the call to the ShouldProcess method must be made from within
     the Process block.

    End
     This block is used to provide optional one-time post-processing for
     the Function.

     The following example shows the outline of a Function that contains a
     Begin block for one-time preprocessing, a Process block for multiple
     record processing, and an End block for one-time post-processing.

         Function Test-ScriptCmdlet
         {
            [CmdletBinding(SupportsShouldProcess=$True)]
            Param ($Parameter1)
            Begin{}
            Process{}
            End{}
         }

Confirmation Methods

    ShouldProcess
     This method is called to request confirmation from the user before the
     Function performs an action that would change the system. The Function
     can continue based on the Boolean value returned by the method. This
     method can be called only from within the Process{} block of the
     Function. And, the CmdletBinding attribute must declare that the
     Function supports ShouldProcess (as shown in the previous example).

     For more information about this method, see
     System.Management.Automation.Cmdlet.ShouldProcess in the MSDN library at
     http://go.microsoft.com/fwlink/?LinkId=142142.

     For more information about how to request confirmation, see
     “Requesting Confirmation” in the MSDN library at
     http://go.microsoft.com/fwlink/?LinkID=136658.

    ShouldContinue
     This method is called to request a second confirmation message. It
     should be called when the ShouldProcess method returns $true. For more
     information about this method, see
     System.Management.Automation.Cmdlet.ShouldContinue in the MSDN library
     at http://go.microsoft.com/fwlink/?LinkId=142143.

Error Methods

    Functions can call two different methods when errors occur. When a
    nonterminating error occurs, the Function should call the WriteError
    method, which is described in the “Write Methods” section. When a
    terminating error occurs and the Function cannot continue, it should call
    the ThrowTerminatingError method. You can also use the Throw statement for
    terminating errors and the Write-Error cmdlet for nonterminating errors.

    For more information, see System.Management.Automation.Cmdlet.
    ThrowTerminatingError in the MSDN libray at
    http://go.microsoft.com/fwlink/?LinkId=142144.

Write Methods

     A Function can call the following methods to return different types of
     output. Notice that not all the output goes to the next command in the
     pipeline. You can also use the various Write cmdlets, such as
     Write-Error.

    WriteCommandDetail
     For information about the WriteCommandDetails method, see
     System.Management.Automation.Cmdlet.WriteCommandDetail in the MSDN
     library at http://go.microsoft.com/fwlink/?LinkId=142155.

    WriteDebug
     To provide information that can be used to troubleshoot a Function,
     make the Function call the WriteDebug method. This displays debug
     messages to the user. For more information, see
     System.Management.Automation.Cmdlet.WriteDebug in the MSDN library
     at http://go.microsoft.com/fwlink/?LinkId=142156.

    WriteError
     Functions should call this method when nonterminating errors occur and
     the Function is designed to continue processing records. For more
     information, see System.Management.Automation.Cmdlet.WriteError in the
     MSDN library at http://go.microsoft.com/fwlink/?LinkId=142157.

     Note: If a terminating error occurs, the Function should call the
            ThrowTerminatingError method.

    WriteObject
     This method allows the Function to send an object to the next command in
     the pipeline. In most cases, this is the method to use when the Function
     returns data. For more information, see
     System.Management.Automation.PSCmdlet.WriteObject in the MSDN library at
     http://go.microsoft.com/fwlink/?LinkId=142158.

    WriteProgress
     For Functions whose actions take a long time to complete, this method
     allows the Function to call the WriteProgress method so that progress
     information is displayed. For example, you can display the percent
     completed. For more information, see
     System.Management.Automation.PSCmdlet.WriteProgress in the MSDN library
     at http://go.microsoft.com/fwlink/?LinkId=142160.

    WriteVerbose
     To provide detailed information about what the Function is doing, make
     the Function call the WriteVerbose method to display verbose messages to
     the user. By default, verbose messages are not displayed. For more
     information, see System.Management.Automation.PSCmdlet.WriteVerbose
     in the MSDN library at http://go.microsoft.com/fwlink/?LinkId=142162.

    WriteWarning
     To provide information about conditions that may cause unexpected
     results, make the Function call the WriteWarning method to display
     warning messages to the user. By default, warning messages are displayed.
     For more information, see
     System.Management.Automation.PSCmdlet.WriteWarning in the MSDN library
     at http://go.microsoft.com/fwlink/?LinkId=142164.

     Note: You can also display warning messages by configuring the
            WarningPreference Variable or by using the Verbose and Debug
            command-line options.

Other Methods and Properties

     For information about the other methods and properties that can be
     accessed through the $PSCmdlet Variable, see
     System.Management.Automation.PSCmdlet in the MSDN library at
     http://go.microsoft.com/fwlink/?LinkId=142139.

     For example, the ParameterSetName property allows you to see the parameter
     set that is being used. Parameter sets allow you to create a Function that
     performs different tasks based on the parameters that are specified when
     the Function is run.

SEE ALSO
    about_functions_advanced
    about_functions_CmdletBindingAttributes
    about_functions_advanced_parameters