Get-SlidingAverage.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
42
43
44
45
46
47
48
function Get-SlidingAverage {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory,ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [array]
        $InputObject
        ,
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Property
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [int]
        $Size = 5
    )

    Begin {
        Write-Debug ('[{0}] Size of queue is <{1}>' -f $MyInvocation.MyCommand, $q.Count)
        $q = New-Object -TypeName System.Collections.Queue -ArgumentList $Size
    }

    Process {
        $InputObject | ForEach-Object {
            if (-Not (Get-Member -InputObject $_ -MemberType Properties -Name $Property)) {
                throw ('[{0}] Unable to find property <{1}> in input object' -f $MyInvocation.MyCommand, $Property)
            }

            #region Enqueue new item and trim to specified size
            $q.Enqueue($_)
            Write-Debug ('[{0}] Size of queue is <{1}>' -f $MyInvocation.MyCommand, $q.Count)
            if ($q.Count -gt $Size) {
                $q.Dequeue() | Out-Null
            }
            #endregion

            #region Calculate average if the specified number of items is present
            if ($q.Count -eq $Size) {
                $q | Microsoft.PowerShell.Utility\Measure-Object -Property $Property -Average | Select-Object -ExpandProperty Average
            }
            #endregion
        }
    }
}

New-Alias -Name gsa -Value Get-SlidingAverage -Force