From 71269705cf4eb273b936ea378768712b0c5abc28 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Thu, 27 Jan 2022 19:35:00 +0100 Subject: [PATCH] Adds feature to sort arrays without ScriptBlocks --- doc/100-General/10-Changelog.md | 1 + .../ConvertTo-IcingaSecureSortedArray.psm1 | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 lib/core/tools/ConvertTo-IcingaSecureSortedArray.psm1 diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index 954c772..b755e3f 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -40,6 +40,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#438](https://github.com/Icinga/icinga-powershell-framework/pull/438) Adds support for enabling the REST-Api background daemon and the Api-Check feature during the IMC installation wizard on advanced settings, which will be enabled by default * [#440](https://github.com/Icinga/icinga-powershell-framework/pull/440) Adds upgrade notification if Icinga for Windows Service binary older than v1.2.0 is used, which will not work with Icinga for Windows v1.8.0 or later * [#445](https://github.com/Icinga/icinga-powershell-framework/pull/445) Adds command `Repair-IcingaService` to repair Icinga Agent service in case it was broken during upgrades, mostly caused by `The specified service has been marked for deletion` +* [#448](https://github.com/Icinga/icinga-powershell-framework/pull/448) Adds support to sort arrays without ScriptBlocks ## 1.7.1 (2021-11-11) diff --git a/lib/core/tools/ConvertTo-IcingaSecureSortedArray.psm1 b/lib/core/tools/ConvertTo-IcingaSecureSortedArray.psm1 new file mode 100644 index 0000000..45e6d09 --- /dev/null +++ b/lib/core/tools/ConvertTo-IcingaSecureSortedArray.psm1 @@ -0,0 +1,56 @@ +<# +.SYNOPSIS + Securely allows to sort arrays for containing objects like hashtables, + which is not simply done by using Sort-Object, but requires custom expressions + (ScriptBlocks) to deal with the sorting. +.DESCRIPTION + Securely allows to sort arrays for containing objects like hashtables, + which is not simply done by using Sort-Object, but requires custom expressions + (ScriptBlocks) to deal with the sorting. + + Icinga for Windows does not allow ScriptBlocks inside external modules while using + JEA profiles. Sorting expressions are ScriptBlocks which are not allowed in JEA + context. To ensure module developers can still sort objects from inside arrays, + this function allows to parse the InputObject as array and adding the name of the + member which should be sorted. In addition it allows configuration if the result + should be sorted Descending or Ascending (default) +.PARAMETER InputObject + An array containing all our objects. Defined as Pipeline input +.PARAMETER MemberName + The member name from within your array objects to sort the result for +.PARAMETER Descending + Set to sort the output result Descending +.EXAMPLE + PS> ConvertTo-IcingaSecureSortedArray -InputObject $MyArray -MemberName 'CreationTime'; +.EXAMPLE + PS> ConvertTo-IcingaSecureSortedArray -InputObject $MyArray -MemberName 'CreationTime' -Descending; +.EXAMPLE + PS> $MyArray | ConvertTo-IcingaSecureSortedArray -MemberName 'CreationTime' -Descending; +#> +function ConvertTo-IcingaSecureSortedArray() +{ + param ( + [Parameter(ValueFromPipeline = $TRUE)] + [array]$InputObject = @(), + [string]$MemberName = '', + [switch]$Descending = $FALSE + ); + + Begin { + [array]$SortedArray = @(); + } + + Process { + if ([string]::IsNullOrEmpty($MemberName)) { + return $InputObject; + } + + foreach ($entry in $InputObject) { + $SortedArray += $entry; + } + } + + End { + return ($SortedArray | Sort-Object -Property @{ Expression = { $_.$MemberName }; Descending = $Descending }); + } +}