Get-InterarrivalTime.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
function Get-InterarrivalTime {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory,ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [array]
        $InputObject
        ,
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Property
        ,
        [Parameter()]
        [ValidateSet('Ticks', 'TotalSecond', 'Minutes', 'Hours', 'Days')]
        [string]
        $Unit = 'Ticks'
    )

    Begin {
        Write-Debug ('[{0}] Entering begin block' -f $MyInvocation.MyCommand)
        $PreviousArrival = $null
    }

    Process {
        Write-Debug ('[{0}] Entering process block' -f $MyInvocation.MyCommand)
        $InputObject | ForEach-Object {
            Write-Debug ('[{0}] Processing value' -f $MyInvocation.MyCommand)
            if ($PreviousArrival) {
                Write-Debug ('[{0}] Calculating interarrival time' -f $MyInvocation.MyCommand)
                Add-Member -InputObject $_ -MemberType NoteProperty -Name "Interarrival$Unit" -Value (New-TimeSpan -Start $PreviousArrival.$Property -End $_.$Property | Select-Object -ExpandProperty $Unit) -PassThru
            }
            $PreviousArrival = $_
        }
    }
}

New-Alias -Name giat -Value Get-InterarrivalTime -Force