Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active May 24, 2024 03:12
Show Gist options
  • Save quonic/34958e11eeb0d78802546dbca58dfd4f to your computer and use it in GitHub Desktop.
Save quonic/34958e11eeb0d78802546dbca58dfd4f to your computer and use it in GitHub Desktop.
Randy Marsh's TMI formula in PowerShell
function Get-TMI {
<#
.SYNOPSIS
Calculates the TMI of a given object.
.DESCRIPTION
Calculates the TMI of a given object.
.PARAMETER Length
The length of the object
.PARAMETER Diameter
The diameter of the object
.PARAMETER Weight
The weight of the object
.PARAMETER Girth
The girth of the object
.PARAMETER AngleOfShaft
The angle of the shaft
.PARAMETER Average
If set, the average of the TMI will be returned when multiple TMIs are provided via the pipeline.
.PARAMETER Sum
If set, the sum of the TMI will be returned when multiple TMIs are provided via the pipeline.
.PARAMETER StandardDeviation
If set, the standard deviation of the TMI will be returned when multiple TMIs are provided via the pipeline.
.EXAMPLE
Get-TMI -Length 6 -Diameter 2 -Weight 4500 -Girth 4 -AngleOfShaft 45
.INPUTS
System.Double
.OUTPUTS
System.Double
#>
[OutputType([System.Double[]])]
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory, ValueFromPipelineByPropertyName)][double]$Length,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)][double]$Diameter,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)][double]$Weight,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)][double]$Girth,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)][double]$AngleOfShaft,
[Parameter()][switch]$Average,
[Parameter()][switch]$Sum,
[Parameter()][switch]$StandardDeviation
)
begin {
$TMIArray = New-Object System.Collections.Generic.List[double]
}
process {
if ($PSCmdlet.ShouldProcess("Calculating TMI")) {
$TMIArray.Add(
$(
$($($Length * $Diameter) + $($Weight / $Girth)) / [math]::Pow($AngleOfShaft, 2)
)
)
}
}
end {
if ($TMIArray.Count -gt 1 -and $Average) {
$TMIArray | Measure-Object -Average | Select-Object -ExpandProperty Average
}
elseif ($TMIArray.Count -gt 1 -and $Sum) {
$TMIArray | Measure-Object -Sum | Select-Object -ExpandProperty Sum
}
elseif ($TMIArray.Count -gt 1 -and $StandardDeviation) {
$TMIArray | Measure-Object -StandardDeviation | Select-Object -ExpandProperty StandardDeviation
}
else {
$TMIArray
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment