Technical Articles
F5 SMEs share good practice.
cancel
Showing results for 
Search instead for 
Did you mean: 
Custom Alert Banner
Joe_Pruitt
Cirrostratus
Cirrostratus

0151T000003d4XrQAI.jpg Welcome to this addition of the PowerShell ABC's where you'll find 26 posts detailing a component of the PowerShell scripting language, one letter at a time.  Today's letter is the letter "C".  For "C" I've picked the most efficient type type of commands supported by PowerShel, the CmdLet.

CmdLets (pronounced "Command-let"), functions, scripts, and native Win32 executables are the four types of commands supported in PowerShell.  A Cmdlet is implemented by a .NET class that derives from the Cmdlet base class in the PowerShell SDK.  This class is compiled into a DLL and loaded into the PowerShell process.  Since a Cmdlet is native code loaded in process, it's the most efficient of the four types of commands to execute.

0151T000003d4XsQAI.jpg Cmdlet's have the names in the form of Verb-Noun, where the verb specifies the action you are performing and the Noun refers to the object you are performing the action on.  Cmdlet's are very similar to builtin commands in many common command shells.

Here is the sample code for a very simple Cmdlet taking from Windows PowerShell in Action by Bruce Payette - a great book by the way!

 

[Cmdlet("Write", "InputObject")]
public class MyWriteInputObjectCmdlet : Cmdlet {

  [Parameter]
public string Parameter1;

[Parameter(Mandatory = true, ValueFromPipeline = true)]
  public string InputObject;

  protected override void ProcessRecord() {
    if ( Parameter1 != null ) {
      WriteObject(Parameter1 + ":" + InputObject);
    } else {
      WriteObject(InputObject);
    }
  }
}

This Cmdlet, Write-InputObject, simply copies it's input to it's output.  If -Parameter1 is specified then it's argument will be used as a prefix on the output string.

Cmdlet's are so great, that I've even written a bunch of iControl Cmdlet's for you BIG-IP users out there.  Check out the PowerShell DevCentral Labs project for the bits and the iControl Apps tech tips I've written that show the goodness that is PowerShell + iControl.

Version history
Last update:
‎16-Dec-2008 07:50
Updated by:
Contributors