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

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

  • Comments 1
  • Likes

Bookmark and Share 


Microsoft Scripting Guy Ed Wilson here. Well, this is truly a historic day for a number of reasons. The first reason is that this is the first Saturday column in Scripting Guy history! The second historic occasion is that we finally got snow in Charlotte, North Carolina. This second reason is not really historicalwe get snow in Charlotte from time to timebut this snow has been long awaited, much anticipated, and truly appreciated. As seen in the following image, even Dr. Scripto got into the spirit of things as he attempted to make a snow person.

Image of Dr. Scripto

Because we received all the snow and had freezing temperatures, I decided I would like to share the information with my friend Georges who lives in Quebec. When sharing such information with my friends outside the United States, I consider it polite to translate the measurements into metric. I was looking through my conversion functions that we discussed back in December’s Hey, Scripting Guy! Why Would I Even Want to Use Functions in My Windows PowerShell Scripts? post , and I decided it would be more convenient to convert the functions into a module, rather than needing to dot-source the file each time I wanted to access them.

The ConversionFunctions.ps1 script is seen here.

ConversionFunctions.ps1

Function Script:ConvertToMeters($feet)
{
  "$feet feet equals $($feet*.31) meters"
} #end ConvertToMeters

Function Script:ConvertToFeet($meters)
{
 "$meters meters equals $($meters * 3.28) feet"
} #end ConvertToFeet

Function Script:ConvertToFahrenheit($celsius)
{
 "$celsius celsius equals $((1.8 * $celsius) + 32 ) fahrenheit"
} #end ConvertToFahrenheit

Function Script:ConvertTocelsius($fahrenheit)
{
 "$fahrenheit fahrenheit equals $( (($fahrenheit - 32)/9)*5 ) celsius"
} #end ConvertTocelsius

Function Script:ConvertToMiles($kilometer)
{
  "$kilometer kilometers equals $( ($kilometer *.6211) ) miles"
} #end convertToMiles

Function Script:ConvertToKilometers($miles)
{
  "$miles miles equals $( ($miles * 1.61) ) kilometers"
} #end convertToKilometers

So how did I create a module from my conversion functions?

1.     The first thing I did was copy all of the functions into a blank Windows PowerShell ISE window.

2.     Next, I renamed all of the functions.

3.     I then created a template so that I could add help to each of the functions.

4.     Last, I used my Copy-Module.ps1 script to install the newly created module onto my system.

Here is the help template I used:

HelpTemplate.txt

<#
  .Synopsis
    Converts into
   .Example
    ConvertTo-
    Converts 1 into
   .Parameter
    The  to be converted
   .Notes
    NAME:  ConvertTo-
    AUTHOR: Ed Wilson
    LASTEDIT: 1/31/2010
    KEYWORDS:
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>

After renaming the functions, and adding help to each function, I saved the module as ConversionModule.psm1. The complete ConversionModule.psm1 module is seen here.

ConversionModule.psm1

Function ConvertTo-Meters
{
 <#
  .Synopsis
    Converts feet into meters
   .Example
    ConvertTo-Meters 1
    Converts 1 foot into meters
   .Parameter feet
    The number of feet to be converted
   .Notes
    NAME:  ConvertTo-Meters
    AUTHOR: Ed Wilson
    LASTEDIT: 1/31/2010
    KEYWORDS:
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>
 [CmdletBinding()]
 param(
      [Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]
      $feet
) #end param
  "$feet feet equals $($feet*.31) meters"
} #end ConvertTo-Meters

Function ConvertTo-Feet
{
 <#
  .Synopsis
    Converts meters into feet
   .Example
    ConvertTo-Feet 1
    Converts 1 meter into feet
   .Parameter meters
    The number of meters to be converted into feet
   .Notes
    NAME:  ConvertTo-Feet
    AUTHOR: Ed Wilson
    LASTEDIT: 1/31/2010
    KEYWORDS:
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>
 [CmdletBinding()]
 param(
      [Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]
      $meters
) #end param
 "$meters meters equals $($meters * 3.28) feet"
} #end ConvertTo-Feet

Function ConvertTo-Fahrenheit
{
 <#
  .Synopsis
    Converts celsius into fahrenheit
   .Example
    ConvertTo-Fahrenheit 1
    Converts 1 degree celsius into fahrenheit
   .Parameter celsius
    The  temperature to be converted into fahrenheit
   .Notes
    NAME:  ConvertTo-Fahrenheit
    AUTHOR: Ed Wilson
    LASTEDIT: 1/31/2010
    KEYWORDS:
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>
 [CmdletBinding()]
 param(
      [Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]
      $celsius
) #end param
 "$celsius celsius equals $((1.8 * $celsius) + 32 ) fahrenheit"
} #end ConvertTo-Fahrenheit

Function ConvertTo-celsius
{
 <#
  .Synopsis
    Converts fahrenheit into celsius
   .Example
    ConvertTo-Celsius 1
    Converts 1 degree fahrenheit into celsius
   .Parameter fahrenheit
    The  temperature to be converted
   .Notes
    NAME:  ConvertTo-Celsius
    AUTHOR: Ed Wilson
    LASTEDIT: 1/31/2010
    KEYWORDS:
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>
 [CmdletBinding()]
 param(
      [Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]
      $fahrenheit
) #end param
 "$fahrenheit fahrenheit equals $( (($fahrenheit - 32)/9)*5 ) celsius"
} #end ConvertT-ocelsius

Function ConvertTo-Miles
{
 <#
  .Synopsis
    Converts kilometers into miles
   .Example
    ConvertTo-Miles
    Converts 1 kilometer into miles
   .Parameter kilometer
    The distance to be converted
   .Notes
    NAME:  ConvertTo-Miles
    AUTHOR: Ed Wilson
    LASTEDIT: 1/31/2010
    KEYWORDS:
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>
 [CmdletBinding()]
 param(
      [Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]
      $kilometer
) #end param
  "$kilometer kilometers equals $( ($kilometer *.6211) ) miles"
} #end convertToMiles

Function ConvertTo-Kilometers
{
 <#
  .Synopsis
    Converts miles into Kilometers
   .Example
    ConvertTo-Kilometers 1
    Converts 1 mile into kilometers
   .Parameter miles
    The distance to be converted
   .Notes
    NAME:  ConvertTo-Kilometers
    AUTHOR: Ed Wilson
    LASTEDIT: 1/31/2010
    KEYWORDS:
   .Link
     Http://www.ScriptingGuys.com
 #Requires -Version 2.0
 #>
 [CmdletBinding()]
 param(
      [Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]
      $miles
) #end param
  "$miles miles equals $( ($miles * 1.61) ) kilometers"
} #end convertTo-Kilometers

After I saved the ConversionModule.psm1 file, I used my Copy-Modules.ps1 script to install my new module. After the module is installed, I like to use the Get-Module cmdlet to ensure it has been properly installed. I also like to import the module by using the Import-Module cmdlet to import the module into the current Windows PowerShell session. I then use the Get-Command cmdlet to see which commands have been exported by the module. This is seen here:

PS C:\> C:\fso\Copy-Modules.ps1

cmdlet Copy-Modules.ps1 at command pipeline position 1
Supply values for the following parameters:
path: c:\fso
PS C:\> Get-Module -ListAvailable

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Script     BasicFunctions            {}
Script     ConversionModule          {}
Script     DotNet                    {}
Manifest   FileSystem                {}
Manifest   IsePack                   {}
Manifest   PowerShellPack            {}
Manifest   PSCodeGen                 {}
Manifest   PSImageTools              {}
Manifest   PSRSS                     {}
Manifest   PSSystemTools             {}
Manifest   PSUserTools               {}
Manifest   TaskScheduler             {}
Manifest   WPK                       {}
Manifest   ActiveDirectory           {}
Manifest   AppLocker                 {}
Manifest   BitsTransfer              {}
Manifest   FailoverClusters          {}
Manifest   GroupPolicy               {}
Manifest   NetworkLoadBalancingCl... {}
Manifest   PSDiagnostics             {}
Manifest   TroubleshootingPack       {}


PS C:\> Import-Module conversion*
PS C:\> Get-Command -Module conversion*

CommandType     Name                                                Definition
-----------     ----                                                ----------
Function        ConvertTo-celsius                                   param($fahrenheit)...
Function        ConvertTo-Fahrenheit                                param($celsius)...
Function        ConvertTo-Feet                                      param($meters)...
Function        ConvertTo-Kilometers                                param($miles)...
Function        ConvertTo-Meters                                    param($feet)...
Function        ConvertTo-Miles                                     param($kilometer)...


PS C:\>

The last thing I do is check the Get-Help function to ensure it is working with my new commands. This is seen here:

PS C:\> Get-Help ConvertTo-kilometers

 

NAME

    ConvertTo-Kilometers

 

SYNOPSIS

    Converts miles into Kilometers

 

SYNTAX

    ConvertTo-Kilometers [-miles] <Object> [<CommonParameters>]

 

DESCRIPTION

 

RELATED LINKS

    Http://www.ScriptingGuys.com

    #Requires -Version 2.0

 

REMARKS

    To see the examples, type: "get-help ConvertTo-Kilometers -examples".

    For more information, type: "get-help ConvertTo-Kilometers -detailed".

    For technical information, type: "get-help ConvertTo-Kilometers -full".

 

PS C:\>

 

I can now tell my friend Georges that the temperature in Charlotte was -3.9 degrees Celsius. This is seen here:

PS C:\> ConvertTo-celsius 25

25 fahrenheit equals -3.88888888888889 celsius

PS C:\>

 

Well, that is how a Scripting Guy spends a snow day in Charlotte, North Carolina.

If you want to know exactly what we will be looking at next week, 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

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Great! We have weekend scripting article now. So glad. Thank you Ed and all the MS scritping guys!!