Weekend Scripter: Conversion Module, Part 6
Microsoft Scripting Guy Ed Wilson here. It has been a relatively quiet weekend here in Charlotte, North Carolina. I spent a couple of hours organizing my dive pictures from a recent trip to Boca Raton, Florida. The spotted eel in the following photo was one of the highlights of that trip.
As I was looking through the pictures, I was also daydreaming a bit about the conversion module we have been working on for the last three weeks. See yesterday’s Weekend Scripter post for links to the other articles in this series.
The problem with modules is they can keep growing on and on and on and on until you feel as if you are painting the Golden Gate Bridge. They are a never-ending proposition. I have decided to call this the final coat of paint, unless I get some specific requests via email@example.com.
As an IT pro and a writer, I am a bit unusual in that I hate to type. Anytime I can reduce the amount of typing I do, I am all in favor of it. Therefore, I decided that I needed to add some aliases to the conversion module. Aliases allow us to follow best practices and give our functions nice descriptive names, but at the same time, we do not have to type the entire function name.
I wanted all of the aliases to follow a specific pattern to make them easier to remember, so I decided to make them all four-letter aliases. To see which ones are available, I used the Get-Alias cmdlet. Because all of the functions begin with convertTo, I decided the first two letters of the alias should be ct. Using the alias for Get-Alias (gal) and a wildcard character after ct, the following command reveals there are no aliases on my computer that match that configuration. This is seen here:
PS C:> gal ct*
I can therefore use any alias that begins with ct for my module. I came up with the following aliases:
New-Alias -Name CTCS -Value ConvertTo-Celsius -Description "Conversion module alias"
New-Alias -Name CTFH -Value ConvertTo-Fahrenheit -Description "Conversion module alias"
New-Alias -Name CTFT -Value ConvertTo-Feet -Description "Conversion module alias"
New-Alias -Name CTKM -Value ConvertTo-Kilometers -Description "Conversion module alias"
New-Alias -Name CTLT -Value ConvertTo-Liters -Description "Conversion module alias"
New-Alias -Name CTMT -Value ConvertTo-Meters -Description "Conversion module alias"
New-Alias -Name CTMS -Value ConvertTo-MetersPerSecond -Description "Conversion module alias"
New-Alias -Name CTML -Value ConvertTo-Miles -Description "Conversion module alias"
New-Alias -Name CTPD -Value ConvertTo-Pounds -Description "Conversion module alias"
By default, a module will only export functions. To cause it to export our newly created aliases, I added the following command:
Export-ModuleMember -alias * -function *
I uploaded the conversionmoduleV6 module to the Script Center Script Repository.
If you want to know exactly what we will be looking at tomorrow, follow us on Twitter and Facebook. If you have any questions, send e-mail to us at firstname.lastname@example.org or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys