Skip to content

Instantly share code, notes, and snippets.

@JPRuskin

JPRuskin/BackToSchool.psd1 Secret

Created Sep 4, 2020
Embed
What would you like to do?
@{
ModuleVersion = "1.0.0"
RequiredModules = @()
FunctionsToExport = @(
"Get-RightAngledTriangleSides"
"Get-CircleArea"
"Get-SphereVolume"
"Get-CylinderVolume"
"Get-Factorial"
)
RootModule = "BackToSchool.psm1"
GUID = "2809e250-f5cf-42b2-874b-2de2a24d6c33"
Description = "Quick attempt at the IronScripter Back to School question"
Author = "@jpruskin"
PowerShellVersion = "5.0"
}
function Get-RightAngledTriangleSides {
<#
.Synopsis
Calculates the length of a missing side of a right-angled triangle
.Example
Get-RightAngledTriangleSides -A 3 -C 5
#>
param(
# The length of side A
$A,
# The length of side B
$B,
# The length of side C
$C
)
end {
switch ($PSBoundParameters.Count) {
2 {
[string[]]$ProvidedSides = $PSBoundParameters.Keys
switch ($Calculating = @("A","B","C").Where{$_ -notin $ProvidedSides}) {
"C" {
# C^2 = A^2 + B^2
$C = [Math]::Sqrt(
[Math]::Pow((Get-Variable -Name $ProvidedSides[0] -ValueOnly), 2) + [Math]::Pow((Get-Variable -Name $ProvidedSides[1] -ValueOnly), 2)
)
}
default {
# X^2 = sqrt(C^2 - Y^2)
$Answer = [Math]::Sqrt(
[Math]::Pow($C,2) - [Math]::Pow((Get-Variable -Name $ProvidedSides.Where{$_ -ne "C"} -ValueOnly),2)
)
Set-Variable -Name $Calculating -Value $Answer
}
}
continue
}
3 {
if (([Math]::Pow($A,2) + [Math]::Pow($B,2)) -ne [Math]::Pow($C,2)) {
Write-Error "Provided Sides Are Not a Valid Right-Angled Triangle"
}
continue
}
default {
Write-Error "Please provide two sides of the triangle" -ErrorAction Stop
}
}
[PSCustomObject]@{
A = $A
B = $B
C = $C
}
}
}
function Get-CircleArea {
<#
.Synopsis
Gets the area of a circle of given radius / diameter
.Example
Get-CircleArea -Radius 3
.Example
Get-CircleArea -Diameter 9
#>
[CmdletBinding()]
param(
# Radius of the circle
[Parameter(Mandatory, ParameterSetName = "Radius")]
$Radius = $($Diameter/2),
# Diameter of the circle
[Parameter(Mandatory, ParameterSetName = "Diameter", ValueFromPipeline)]
$Diameter
)
process {
Write-Verbose "The area of a circle is 'π r^2'"
Write-Verbose "Where:"
Write-Verbose " π = $([Math]::Pi)"
switch ($PSCmdlet.ParameterSetName) {
"Diameter" {Write-Verbose " r = ($Diameter / 2)"}
"Radius" {Write-Verbose " r = $Radius"}
}
Write-Verbose "r^2 = $([Math]::Pow($Radius, 2))"
[Math]::Pi * [Math]::Pow($Radius, 2)
}
}
function Get-SphereVolume {
<#
.Synopsis
Calculates the volume of a sphere, given the radius
.Example
Get-SphereVolume -Radius 3
#>
[CmdletBinding()]
param(
# Radius of the sphere
[Parameter(Mandatory, ValueFromPipeline)]
$Radius
)
process {
Write-Verbose "The volume of a sphere is '4/3 π r^3'"
Write-Verbose "Where:"
Write-Verbose " π = $([Math]::Pi)"
Write-Verbose " r = $($Radius)"
Write-Verbose "r^3 = $([Math]::Pow($Radius, 3))"
4/3 * [Math]::Pi * [Math]::Pow($Radius,3)
}
}
function Get-CylinderVolume {
<#
.Synopsis
Calculates the volume of a cylinder, given the radius and height
.Example
Get-CylinderVolume -R 3 -H 5
#>
[CmdletBinding()]
param(
# Radius of the cylinder
[Parameter(Mandatory)]
$Radius,
# Height of the cylinder
[Parameter(Mandatory)]
$Height
)
process {
Write-Verbose "The volume of a cylinder is 'π r^2 h', or the area of the circular face multiplied by the height"
$CircleArea = Get-CircleArea -Radius $Radius
Write-Verbose " h = $($Height)"
$Height * $CircleArea
}
}
function Get-Factorial {
<#
.Synopsis
Calculates the factorial value for a value of N
#>
param(
# Value to calculate for
[Parameter(Mandatory, ValueFromPipeline)]
[uint16]$N
)
process {
$Total = 1
while ($N -gt 0) {
$Total *= $N--
}
$Total
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment