|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Securely allows to sort arrays for containing objects like hashtables, |
| 4 | + which is not simply done by using Sort-Object, but requires custom expressions |
| 5 | + (ScriptBlocks) to deal with the sorting. |
| 6 | +.DESCRIPTION |
| 7 | + Securely allows to sort arrays for containing objects like hashtables, |
| 8 | + which is not simply done by using Sort-Object, but requires custom expressions |
| 9 | + (ScriptBlocks) to deal with the sorting. |
| 10 | +
|
| 11 | + Icinga for Windows does not allow ScriptBlocks inside external modules while using |
| 12 | + JEA profiles. Sorting expressions are ScriptBlocks which are not allowed in JEA |
| 13 | + context. To ensure module developers can still sort objects from inside arrays, |
| 14 | + this function allows to parse the InputObject as array and adding the name of the |
| 15 | + member which should be sorted. In addition it allows configuration if the result |
| 16 | + should be sorted Descending or Ascending (default) |
| 17 | +.PARAMETER InputObject |
| 18 | + An array containing all our objects. Defined as Pipeline input |
| 19 | +.PARAMETER MemberName |
| 20 | + The member name from within your array objects to sort the result for |
| 21 | +.PARAMETER Descending |
| 22 | + Set to sort the output result Descending |
| 23 | +.EXAMPLE |
| 24 | + PS> ConvertTo-IcingaSecureSortedArray -InputObject $MyArray -MemberName 'CreationTime'; |
| 25 | +.EXAMPLE |
| 26 | + PS> ConvertTo-IcingaSecureSortedArray -InputObject $MyArray -MemberName 'CreationTime' -Descending; |
| 27 | +.EXAMPLE |
| 28 | + PS> $MyArray | ConvertTo-IcingaSecureSortedArray -MemberName 'CreationTime' -Descending; |
| 29 | +#> |
| 30 | +function ConvertTo-IcingaSecureSortedArray() |
| 31 | +{ |
| 32 | + param ( |
| 33 | + [Parameter(ValueFromPipeline = $TRUE)] |
| 34 | + [array]$InputObject = @(), |
| 35 | + [string]$MemberName = '', |
| 36 | + [switch]$Descending = $FALSE |
| 37 | + ); |
| 38 | + |
| 39 | + Begin { |
| 40 | + [array]$SortedArray = @(); |
| 41 | + } |
| 42 | + |
| 43 | + Process { |
| 44 | + if ([string]::IsNullOrEmpty($MemberName)) { |
| 45 | + return $InputObject; |
| 46 | + } |
| 47 | + |
| 48 | + foreach ($entry in $InputObject) { |
| 49 | + $SortedArray += $entry; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + End { |
| 54 | + return ($SortedArray | Sort-Object -Property @{ Expression = { $_.$MemberName }; Descending = $Descending }); |
| 55 | + } |
| 56 | +} |
0 commit comments