forked from akpaevj/OneSTools.PS.TechLog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup-TechLog.ps1
More file actions
67 lines (58 loc) · 1.89 KB
/
Group-TechLog.ps1
File metadata and controls
67 lines (58 loc) · 1.89 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function Group-TechLog {
[CmdletBinding()]
[OutputType([PSCustomObject])]
Param (
[Parameter(Mandatory=$true,
Position = 0,
ValueFromPipeline=$true)]
[OneSTools.PS.TechLog.TjEvent]$InputObject,
[Parameter(Mandatory=$true,
ValueFromPipeline=$true)]
[Alias("GP")]
$GroupProperty,
[Parameter(Mandatory=$true,
ValueFromPipeline=$true)]
[Alias("AP")]
$AggregationProperty
)
begin {
$property = [OneSTools.PS.TechLog.TjEvent].GetProperty("$GroupProperty")
if ($null -eq $property) {
throw "TjEvent type doesn't contain property that's named $GroupProperty"
} else {
$propertyType = $property.PropertyType.FullName
Write-Verbose("Grouping property type is $propertyType")
$inputObjects = New-Object "Collections.Generic.Dictionary[$propertyType,object]"
}
}
process {
$group = $InputObject."$GroupProperty";
if ([string]::IsNullOrEmpty($group)) {
return
};
$aggr = [double]$InputObject."$AggregationProperty";
if ($InputObjects.ContainsKey($group)) {
$a = $inputObjects[$group];
$a.Sum += $aggr;
$a.Count += 1;
$a.Min = [math]::Min($a.Min, $aggr);
$a.Max = [math]::Max($a.Max, $aggr);
}
else {
$inputObjects.Add($group, @{Sum = $aggr; Count = 1; Min = $aggr; Max = $aggr});
}
}
end {
foreach($kv in $inputObjects.GetEnumerator()) {
$v = $kv.Value;
[PSCustomObject]@{
Name = $kv.Key
Sum = $v.Sum
Avg = $v.Sum / $v.Count
Min = $v.Min
Max = $v.Max
Count = $v.Count
}
}
}
}