Source/Get-ApplicationRatio.ps1

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
Function Get-GreatestCommonDivisor
{
    [alias('gcd')]
    param(
        [System.Collections.ArrayList] $Numbers
    )

    $Minimum = ($Numbers | Measure-Object -Minimum).Minimum
    foreach ($Remainder in $Numbers.foreach( { $_ % $Minimum }) )
    {
        # if remainder is greater than 0, that means a number is not completely divisible
        if ($Remainder -ne 0)
        {
            return 1 # in that case, greatest common divisor would be 1
        }
    }
    return $Minimum # if there are no remainders
}

Function Get-ApplicationRatio{
    [cmdletbinding()]
    [Alias('Ratio')]
    Param($String)
    $Rows = $String.split(',')
    for($i=0;$i -lt $Rows.count;$i++){
        $Row = ($Rows[$i]).split('*')
        $Row = $Row[0..$($Row.count-2)]
        $Ratio = Foreach($Item in $Row){
            if($Item -eq ''){
                "1"
            }else{
                $item
            }
        }
        $GreatestCommonDivisor = Get-GreatestCommonDivisor $Ratio
        $Ratio.ForEach({$_/$GreatestCommonDivisor}) -join ':'
        Write-Verbose $("Row{0} application ratio is {1}" -f ($i+1),($Ratio -join ':') )
    }
}