Invoking Cmdlets and Scripts Within a Cmdlet

 

A cmdlet can invoke other cmdlets and scripts from within the input processing method of the cmdlet. This allows you to add the functionality of existing cmdlets and scripts to your cmdlet without having to rewrite the code.

All cmdlets can invoke an existing cmdlet by calling the Overload:System.Management.Automation.Cmdlet.Invoke method from within an input processing method, such as BeginProcessing, that is overridden by the cmdlet. However, you can invoke only those cmdlets that derive directly from the Cmdlet class. You cannot invoke a cmdlet that derives from the PSCmdlet class.

The Overload:System.Management.Automation.Cmdlet.Invoke method has the following variants.

Cmdlet.Invoke

This variant invokes the cmdlet object and returns a collection of "T" type objects.

Cmdlet.Invoke<T>

This variant invokes the cmdlet object and returns a strongly typed emumerator. This variant allows the user to use the objects in the collection to perform custom operations.

Example

Description

Invoking Cmdlets Within a Cmdlet

This example shows how to invoke a cmdlet from within another cmdlet.

Invoking Scripts Within a Cmdlet

This example shows how to invoke a script that is supplied to the cmdlet from within another cmdlet.

Show: