Adds feature to sort arrays without ScriptBlocks

This commit is contained in:
Lord Hepipud 2022-01-27 19:35:00 +01:00
parent d525b22cc1
commit 71269705cf
2 changed files with 57 additions and 0 deletions

View file

@ -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 * [#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 * [#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` * [#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) ## 1.7.1 (2021-11-11)

View file

@ -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 });
}
}