Skip to content

Commit 335038c

Browse files
authored
Merge pull request #448 from Icinga:feature/secure_array_sorting_external_modules
Feature: Adds support to sort arrays without ScriptBlocks Adds a new function, allowing to sort arrays with objects inside for specific members wihtout having to use ScriptBlocks on external modules. This allows a secure usage even in JEA context and external modules are not blocked by calling `ScriptBlocks`
2 parents d525b22 + 7126970 commit 335038c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
4040
* [#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
4141
* [#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
4242
* [#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`
43+
* [#448](https://github.com/Icinga/icinga-powershell-framework/pull/448) Adds support to sort arrays without ScriptBlocks
4344

4445
## 1.7.1 (2021-11-11)
4546

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)