Make-A-Cmdlet: Part 6, Wrapup

Okay, in my cmdlet code, you’ll notice that I used WMI – albeit in a very .NET-esque fashion – to ping a remote computer. That’s kind of my VBScript background talking; WMI is how I’d do the same task in VBScript. Well, it pays to investigate: Turns out .NET has a built-in functioning for pinging computers! I can change my cmdlet’s subroutine to be even simpler:

Protected Overrides Sub ProcessRecord()
 Dim ThisName As String
 For Each ThisName In _Name
  If My.Computer.Network.Ping(ThisName) Then
   WriteObject(ThisName)
  End If
 Next
End Sub

This is a good example of how a little exploration can save you a lot of time coding. My entire cmdlet now consists of a property declaration and this subroutine – about a dozen lines of VB.NET code!

I also mentioned earlier that you could have been doing all this programming in PrimalScript 4.1 Enterprise. I’ve been using Visual Basic Express, which is a free, stripped-down edition of Visual Studio 2005; I did so mainly because there are templates available to get me started with a snapin and cmdlet. So where are the templates for PrimalScript? They’re coming :). I’ll use PrimalScript for future projects, in fact – after all, I already have it installed, and it takes up about a third the drive space that even the stripped-down Visual Basic Express does. On my laptop in particular, drive space is still at a bit of a premium (I’m an ultraportable guy – you can keep your 17″ widescreen!).

So you’ve seen how to build a cmdlet. You may have noticed that I named my cmdlet “PrimalToys.” The name comes from a collection of COM objects that are included with PrimalScript (bet you didn’t know you had them – they’re documented in the PrimalScript 4.1: TFM manual, which is accessible from within the product’s Help menu). PrimalScript even includes an MSI-packaged version of PrimalToys (in the PrimalScript installation folder, under the Redist subfolder) so you can make the components available everywhere. PrimalToys seemed like a natural name for my PowerShell cmdlet package, then. Yes, package – you’ve only seen one, but I have about two-dozen in mind. Stay tuned, because I’m going to share the source code from this project as an attachment in a future post next week.

Technorati Tags: