mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
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`
This commit is contained in:
commit
335038c957
2 changed files with 57 additions and 0 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
56
lib/core/tools/ConvertTo-IcingaSecureSortedArray.psm1
Normal file
56
lib/core/tools/ConvertTo-IcingaSecureSortedArray.psm1
Normal 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 });
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue