A while back, I wrote a bit of a rant about the difficulties in finding helpful resources on writing PowerShell 2.0 cmdlets in C#. In case you hadn’t read it, my point was that there’s literally tons of great resources on authoring PowerShell scripts, but very little comparatively on how to author your own binary script modules. I still stand by that assessment regarding binary modules, however, I received some great advice recently that really helped me understand the learning curve involved in transitioning from a PowerShell scripter to a module author, and I’d like to pass that advice along.
During my search for resources on authoring PoSH 2.0 in C#, I reached out to my friend Nick Weaver. Nick is a great guy, extremely smart, and helpful. Recently he authored some binary PowerShell modules to manage EMC Celerra storage, and I asked him what his process was in writing the modules. His response was one I hadn’t considered before, but actually makes perfect sense. He told me to accomplish what I want to do in the console first, THEN move to writing the C# code. For me, his answer was one of those forehead slapping moments — of course, why didn’t I think of that?! 🙂
Following his advice, I discarded my previous goal of writing my module in C#, and instead focused on understanding how to properly write a script module. In doing that, I learned much more about PowerShell’s advanced functions, passing & validating parameters, processing the pipeline, writing help, & authoring module manifests. I must say that breaking the work & learning down into smaller chunks made the whole process easier and more exciting. The great thing about PowerShell is that it’s based on .Net, and believe it or not, much of the same coding flows exist between writing script modules & binary modules. Once you’ve understood the advanced features and functionality that PowerShell offers, learning the code “behind the scenes” will be that much easier.
So, in regards to the title of this post here are some tips for writing your first PowerShell cmdlets & modules.
1.) Break the learning work down into smaller steps. Don’t try to stack learning on top of learning — understand the basics first (pipelining, parameters, error handling, cool things like -Confirm and -WhatIf), and THEN start learning the C#.
2.) Read the PowerShell help. All the help that’s available from the PowerShell command line is also available on TechNet. Personally, I found it easier to read on a webpage vs reading in a command prompt.
3.) Read and re-read until you have a firm grasp on about_Functions_Advanced, about_Functions_Advanced_Methods, about_Functions_Advanced_Parameters, and about_Functions_Cmdlet_Binding_Attribute.
4.) If you plan on packaging your code as a module, read & understand modules and how to create a module manifest.
5.) Use all of the community resources available to you. There are tons of great blogs, podcasts, and friendly folks from the community on Twitter. Most folks have a desire to help, and are willing to spend a few moments pointing you in the right direction. It’s no substitute for your own healthy intellectual curiosity, meaning most folks are happy to help those who help themselves.
6.) Last, but not least, if you learn something, pass it on. 🙂