Hey, Scripting Guy! Weekend Scripter: Conversion Module, Part 5

ScriptingGuy1

Bookmark and Share

 

Microsoft Scripting Guy Ed Wilson here. What a wonderful day Saturday is. I took the occasion to upgrade from the current beta of Microsoft Office 2010 to the release candidate. I love Microsoft Office 2010, and Outlook in particular has received a very nice upgrade. This week, I was tweeting with Megan (@OrlandoTechNuts on Twitter), and she introduced me to a band called The Script. I figured as a Scripting Guy I had to listen to them. They are great, and I have been listening to them ever since. Because Megan is from San Francisco, I’m including the following picture, which I took in San Francisco when I was teaching a WMI workshop to a group of Microsoft customers.

Image of one of San Francisco's most famous buildings

 

Today’s article is part five of the conversion module we began during the inaugural Weekend Scripter article. The second article added additional help information to the functions to better integrate with the Get-Help cmdlet. The third article added a function that converts several different types of input into pounds. The fourth article added a function that converts input into liters.

In today’s article, we add an additional function, but we also change how the data is returned from each of the functions. In all the previous versions of the conversion module, the data that is returned is a string. This means that if you wanted to use the results of the module in a computation, you would need to parse a string. This could be done, of course, but it is a lot of extra work.

Because we are already familiar with the object-oriented nature of Windows PowerShellthe cmdlets return objectsand not merely text, it makes sense to restructure our module to also return an object. The new function, ConvertTo-MetersPerSecond, is seen here.

ConvertTo-MetersPerSecond

Function ConvertTo-MetersPerSecond
{
 <#
  .Synopsis
    Converts Miles per Hour into Meters per second. 
   .Example
    ConvertTo-MetersPerSecond -MilesPerHour 55
    Converts 55 miles per hour into meters per second
   .Parameter MilesPerHour
    The Miles per hour to be converted to Meters per Second
   .Inputs
    [double]
   .Outputs
    [psobject]
   .Notes
    NAME:  ConvertTo-MetersPerSecond
    AUTHOR: Ed Wilson
    AUTHOR BOOK: Windows PowerShell 2.0 Best Practices, Microsoft Press 2010
    LASTEDIT: 1/31/2010
    KEYWORDS: WeekEnd Scripter, Modules, Getting Started
   .Link
     Http://www.ScriptingGuys.com
     Http://www.bit.ly/HSGBlog
 #>
 #Requires -Version 2.0
[CmdletBinding()]
 param(
      [Parameter(Mandatory = $True,valueFromPipeline=$true)]
      [Double]
      $MilesPerHour
) #end param

if($MilesPerHour)
  {
   $outObject = New-Object psobject -property @{
        value = ($milesPerHour * 0.44704) ;
        units = “Meters per sec”
   }
   $outObject
  }
} #end function ConvertTo-MetersPerSecond

The use of the help tags and parameters have already been discussed in our previous articles. The critical change is the creation of a new psobject by using the New-Object cmdlet, and adding the properties for that object. The properties of the new object are added via a hash table, @{}. A semicolon separates each property. It is then important to return the entire object back to the calling code. This portion of the function is seen here:

$outObject = New-Object psobject -property @{
        value = ($milesPerHour * 0.44704) ;
        units = “Meters per sec”
   }
   $outObject

After installing the module, you can import the module and use the functions in the following manner:

PS C:> Import-Module conv*

PS C:> $a = ConvertTo-MetersPerSecond -MilesPerHour 55

PS C:> $a.value

24.5872

PS C:> $a.units

Meters per sec

PS C:> “the result is $($a.value) $($a.units)”

the result is 24.5872 Meters per sec

PS C:>

Testing the various functions is seen in the following image.

Image of testing various functions

 

Rather than overload this blog post with the rapidly lengthening conversion module, I have uploaded the conversion module version 5 to the Script Center Repository.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

 

0 comments

Discussion is closed.

Feedback usabilla icon