Scripting

Hey, Scripting Guy! Weekend Scripter: Using Functions to Convert Weight Measurements (Part 2)

ScriptingGuy1

Microsoft Scripting Guy Ed Wilson here. I am still hanging out down here in Murrells Inlet, South Carolina, in the United States of America. The laid-back, relaxed attitude has allowed me time to reflect on the new Weekend Scripter series I am writing. The following seagull picture I took seems to reflect this deep thought. Looks pensive, no?

Yesterday, I added the ConvertTo-Pounds function to the ConversionModule.psm1 module. In that function, multiple parameters were created to allow the conversion of different units of measurement to pounds. The same technique will be used now to add a ConvertTo-Liters function to the ConversionModule.psm1 module. I have incremented the name of the ConversionModule.psm1 module to allow for easy version tracking during this project. Before copying the module to the modules directory, I have been deleting the previous version of the module to make it easy to load and test the cmdlets.

The new ConvertTo-Liters function is seen here.

ConvertTo-Liters

Function ConvertTo-Liters
{
<#
.Synopsis
Converts cubic centimeters, cubic feet, gallons, pints, and quarts into liters
.Description
The ConvertTo-Liters function will accept an input in
cubic centimeters, cubic feet, gallons, pints, and quarts and convert to liters.
.Example
ConvertTo-Liters -cCentimeter 100
Converts 100 cubic centimeters into liters
.Example
ConvertTo-Liters -cFeet 10
Converts 10 cubic feet into liters
.Example
ConvertTo-Liters -Gallon 1
Converts 1 gallon into liters
.Example
ConvertTo-Liters -pint 5
Converts 5 pints into liters
.Example
ConvertTo-Liters -quart 5
Converts 5 quarts into liters
.Parameter cCentimeter
The number of cubic centimeters to be converted
.Parameter cFeet
The number of cubic feet to be converted
.Parameter Gallon
The number of gallons to be converted
.Parameter Pint
The number of pints to be converted
.Parameter Quart
The number of quarts to be converted
.Inputs
[double]
.Outputs
[string]
.Notes
NAME:  ConvertTo-Liters
AUTHOR: Ed Wilson
AUTHOR BOOK: Windows PowerShell 2.0 Best Practices, Microsoft Press 2010
LASTEDIT: 2/9/2010
KEYWORDS: WeekEnd Scripter, modules, getting started
Http://www.ScriptingGuys.com
Http://www.bit.ly/HSGBlog
#>
#Requires -Version 2.0
[CmdletBinding()]
param(
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$cCentimeter,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$cFeet,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$Gallon,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$Quart,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$Pint
) #end param

If(\$cCentimeter)
{ “\$cCentimeter cubic centimeters equals \$(\$cCentimeter * [math]::pow(10,-3)) liters” }
If(\$cFeet)
{ “\$cFeet cubic feet equals \$(\$cFeet * 28.316) liters” }
If(\$Gallon)
{ “\$Gallon gallons equals \$(\$Gallon * 3.7853) liters” }
If(\$Quart)
{ “\$Quart quarts equals \$(\$Quart * 0.94633) liters” }
If(\$Pint)
{ “\$Pint pints equals \$(\$Pint * 0.47316) liters” }
} #end ConvertTo-Liters

The ConvertTo-Liters function will accept cubic centimeters, cubic feet, gallons, quarts, and pints for input. It will perform a conversion and display the result in liters. The cubic centimeters and the cubic feet parameters use cCentimeter and cFeet for their parameter names. Most of the work involved in creating the ConvertTo-Liters function is similar to the ConvertTo-Pounds function that was created yesterday. The one thing that is a bit different is found in the conversion to cubic centimeters. The formula is cCentimeter * 10-3 . To do that, you need to use the pow static method from the system.math .NET Framework class, which is documented on MSDN and provides a number of static methods. The pow static method is used to create powers. To access a static method from the system.math class, put the word math in square brackets and use double colons to call the method. This is seen here:

PS C:> 25 * [math]::pow(10,-3)
0.025
PS C:>

After the new function has been added to the ConversionModule, the function can be accessed as seen in the following image.

The complete ConversionModuleV4.psm1 module is seen here.

ConversionModuleV4.psm1

Function ConvertTo-Liters
{
<#
.Synopsis
Converts cubic centimeters, cubic feet, gallons, pints, and quarts into liters
.Description
The ConvertTo-Liters function will accept an input in
cubic centimeters, cubic feet, gallons, pints, and quarts and convert to liters.
.Example
ConvertTo-Liters -cCentimeter 100
Converts 100 cubic centimeters into liters
.Example
ConvertTo-Liters -cFeet 10
Converts 10 cubic feet into liters
.Example
ConvertTo-Liters -Gallon 1
Converts 1 gallon into liters
.Example
ConvertTo-Liters -pint 5
Converts 5 pints into liters
.Example
ConvertTo-Liters -quart 5
Converts 5 quarts into liters
.Parameter cCentimeter
The number of cubic centimeters to be converted
.Parameter cFeet
The number of cubic feet to be converted
.Parameter Gallon
The number of gallons to be converted
.Parameter Pint
The number of pints to be converted
.Parameter Quart
The number of quarts to be converted
.Inputs
[double]
.Outputs
[string]
.Notes
NAME:  ConvertTo-Liters
AUTHOR: Ed Wilson
AUTHOR BOOK: Windows PowerShell 2.0 Best Practices, Microsoft Press 2010
LASTEDIT: 2/9/2010
KEYWORDS: WeekEnd Scripter, modules, getting started
Http://www.ScriptingGuys.com
Http://www.bit.ly/HSGBlog
#>
#Requires -Version 2.0
[CmdletBinding()]
param(
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$cCentimeter,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$cFeet,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$Gallon,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$Quart,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$Pint
) #end param

If(\$cCentimeter)
{ “\$cCentimeter cubic centimeters equals \$(\$cCentimeter * [math]::pow(10,-3)) liters” }
If(\$cFeet)
{ “\$cFeet cubic feet equals \$(\$cFeet * 28.316) liters” }
If(\$Gallon)
{ “\$Gallon gallons equals \$(\$Gallon * 3.7853) liters” }
If(\$Quart)
{ “\$Quart quarts equals \$(\$Quart * 0.94633) liters” }
If(\$Pint)
{ “\$Pint pints equals \$(\$Pint * 0.47316) liters” }
} #end ConvertTo-Liters

Function ConvertTo-Pounds
{
<#
.Synopsis
Converts tons, ounces, kilograms, or metric tons into pounds
.Description
The ConvertTo-Pounds function will accept an input in
tons, ounces, kilograms or metric tons and convert the result into pounds.
.Example
ConvertTo-Pounds -ton 1
Converts 1 ton into pounds
.Example
ConvertTo-Pounds -ounce 1000
Converts 1000 ounces into pounds
.Example
ConvertTo-Pounds -kilogram 1
Converts 1 kilograms into pounds
.Example
ConvertTo-Pounds -metricTon 1
Converts 1 metricTon into pounds
.Parameter ton
The number of tons to be converted
.Parameter ounce
The number of ounces to be converted
.Parameter kilogram
The number of kilograms to be converted
.Parameter metricTon
The number of metricTons to be converted
.Inputs
[double]
.Outputs
[string]
.Notes
NAME:  ConvertTo-Pounds
AUTHOR: Ed Wilson
AUTHOR BOOK: Windows PowerShell 2.0 Best Practices, Microsoft Press 2010
LASTEDIT: 1/31/2010
KEYWORDS: WeekEnd Scripter, modules, getting started
Http://www.ScriptingGuys.com
Http://www.bit.ly/HSGBlog
#>
#Requires -Version 2.0
[CmdletBinding()]
param(
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$Ton,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$Ounce,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$KiloGram,
[Parameter(Mandatory = \$false,valueFromPipeline=\$true)]
[Double]
\$MetricTon
) #end param
If(\$ton)
{ “\$ton tons equals \$(\$ton * 2000) pounds” }
&nbs p; If(\$ounce)
{ “\$ounce ounces equals \$(\$ounce * 0.0625) pounds” }
If(\$kilogram)
{ “\$kilogram kilograms equals \$(\$kilogram * 2.205) pounds” }
If(\$metricTon)
{ “\$metricTon metric tons equals \$(\$metricTon * 2205) pounds” }
} #end ConvertTo-Pounds

Function ConvertTo-Meters
{
<#
.Synopsis
Converts feet into meters
.Description
The ConvertTo-Meters function accepts a value in feet and
returns a string indicating the number of meters.
.Example
ConvertTo-Meters 1
Converts 1 foot into meters
.Parameter feet
The number of feet to be converted
.Inputs
[double]
.Outputs
[string]
.Notes
NAME:  ConvertTo-Meters
AUTHOR: Ed Wilson
AUTHOR BOOK: Windows PowerShell 2.0 Best Practices, Microsoft Press 2010
LASTEDIT: 1/31/2010
KEYWORDS: WeekEnd Scripter, modules, getting started
Http://www.ScriptingGuys.com
Http://www.bit.ly/HSGBlog
#Requires -Version 2.0
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = \$true,Position = 0,valueFromPipeline=\$true)]
[Double]
\$feet
) #end param
“\$feet feet equals \$(\$feet*.31) meters”
} #end ConvertTo-Meters

Function ConvertTo-Feet
{
<#
.Synopsis
Converts meters into feet
.Description
The ConvertTo-Feet function accepts a value in meters and
returns a string indicating the number of feet.
.Example
ConvertTo-Feet 1
Converts 1 meter into feet
.Parameter meters
The number of meters to be converted into feet
.Inputs
[double]
.Outputs
[string]
.Notes
NAME:  ConvertTo-Feet
AUTHOR: Ed Wilson
LASTEDIT: 1/31/2010
KEYWORDS: WeekEnd Scripter, modules, getting started
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = \$true,Position = 0,valueFromPipeline=\$true)]
[Double]
\$meters
) #end param
“\$meters meters equals \$(\$meters * 3.28) feet”
} #end ConvertTo-Feet

Function ConvertTo-Fahrenheit
{
<#
.Synopsis
Converts Celsius into Fahrenheit
.Description
The ConvertTo-Fahrenheit function accepts a value in Celsius and
returns a string indicating the temperature in Fahrenheit.
.Example
ConvertTo-Fahrenheit 1
Converts 1 degree Celsius into Fahrenheit
.Parameter celsius
The  temperature to be converted into Fahrenheit
.Inputs
[double]
.Outputs