Search
Joe Pruitt - A Software Architect's take on Network Security
You are here: DevCentral > Weblogs

posted on Tuesday, December 16, 2008 8:50 AM

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

cmdlet-get-executionpolicy 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.



Feedback

No comments posted yet.

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 6 and 1 and type the answer here:

Blog Stats

Posts:379
Comments:1067
Stories:1
Trackbacks:301
  

Article Categories

  iRules
  

Image Galleries

  

Joe's bookshelf: read

The Lost Gate
4 of 5 stars
This one started slow but I got really got into it about 1/3 of the way through. If you are an Ender's Game fan, you'll probably like this one as well.

goodreads.com


82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or