-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert-Temperature.ps1
More file actions
53 lines (42 loc) · 1.18 KB
/
Convert-Temperature.ps1
File metadata and controls
53 lines (42 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Function Convert-Temperature {
<#
.SYNOPSIS
Converts between Celsius and Fahrenheit.
.DESCRIPTION
Converts temperatures between Celsius and Fahrenhiet, vice-versa.
.PARAMETER Celsius
Converts the Value to Celsius.
.PARAMETER Fahrenheit
Coverts the Value to Fahrenheit.
#>
[CmdletBinding()]
param (
[Parameter(
Mandatory,
Position = 0,
ValueFromPipeline = $true)]
[double[]]
$Value,
[Parameter(ParameterSetName = 'Celsius')]
[switch]
$Celsius,
[Parameter(ParameterSetName = 'Fahrenheit')]
[switch]
$Fahrenheit
)
BEGIN {
if ($Fahrenheit.IsPresent) {
[string]$formula = "(( 9 * tempValue) / 5) + 32"
}
else {
[string]$formula = "(5 * (tempValue - 32)) / 9"
}
}
PROCESS {
foreach ($temperature in $Value) {
[double]$result = Invoke-Expression (($formula).Replace('tempValue', $temperature))
[double]$roundedResult = [System.Math]::Round($result, 2)
Write-Output $roundedResult
}
}
}