|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Compares an InputObject of type [array] or [string] to a specified |
| 4 | + include and exclude array filter, returning either if the filter applies |
| 5 | + as true/false or an object of type [array] for filtered InputObject values |
| 6 | +.DESCRIPTION |
| 7 | + Compares an InputObject of type [array] or [string] to a specified |
| 8 | + include and exclude array filter, returning either if the filter applies |
| 9 | + as true/false or an object of type [array] for filtered InputObject values. |
| 10 | +
|
| 11 | + The function is designed to work as a general filter approach for check plugins as |
| 12 | + example, to ensure filtering certain values is easy. After comparing the include |
| 13 | + and exclude filter, the function will return True if the input object can be included |
| 14 | + and will return False in case it should not be included. |
| 15 | +
|
| 16 | + For [array] objects, the function will return a filtered [array] object on which all |
| 17 | + values which should be included and excluded were evaluated and only the remaining ones |
| 18 | + are returned. |
| 19 | +.PARAMETER InputObject |
| 20 | + The object to compare the filter against. This can be of type [array] or [string]. |
| 21 | + Using an [array] object will return a filtered [array] result, while a [string] value |
| 22 | + will return a [bool], if the filter can be applied or not |
| 23 | +.PARAMETER Include |
| 24 | + An [array] of values to compare the InputObject against and include only certain input |
| 25 | +.PARAMETER Exclude |
| 26 | + An [array] of values to compare the InputObject against and exclude only certain input |
| 27 | +.EXAMPLE |
| 28 | + PS> Test-IcingaArrayFilter -InputObject @('icinga2', 'icingapowershell', 'winrm') -Exclude @('icinga2'); |
| 29 | +
|
| 30 | + icingapowershell |
| 31 | + winrm |
| 32 | +.EXAMPLE |
| 33 | + PS> Test-IcingaArrayFilter -InputObject @('icinga2', 'icingapowershell', 'winrm') -Exclude @('*icinga*'); |
| 34 | +
|
| 35 | + winrm |
| 36 | +.EXAMPLE |
| 37 | + PS> Test-IcingaArrayFilter -InputObject 'icinga2' -Include @('*icinga*', 'winrm'); |
| 38 | +
|
| 39 | + True |
| 40 | +.EXAMPLE |
| 41 | + PS> Test-IcingaArrayFilter -InputObject 'icinga2' -Include @('*icinga*', 'winrm') -Exclude @('*icinga*'); |
| 42 | +
|
| 43 | + False |
| 44 | +#> |
| 45 | + |
| 46 | +function Test-IcingaArrayFilter() |
| 47 | +{ |
| 48 | + param ( |
| 49 | + $InputObject = $null, |
| 50 | + [array]$Include = @(), |
| 51 | + [array]$Exclude = @() |
| 52 | + ); |
| 53 | + |
| 54 | + [bool]$ReturnArray = $FALSE; |
| 55 | + |
| 56 | + if ($InputObject -Is [array]) { |
| 57 | + $ReturnArray = $TRUE; |
| 58 | + } |
| 59 | + |
| 60 | + [array]$FilteredArray = @(); |
| 61 | + |
| 62 | + if ($null -eq $InputObject) { |
| 63 | + if ($ReturnArray) { |
| 64 | + return $InputObject; |
| 65 | + } |
| 66 | + |
| 67 | + return $FALSE; |
| 68 | + } |
| 69 | + |
| 70 | + if ($Include.Count -eq 0 -And $Exclude.Count -eq 0) { |
| 71 | + if ($ReturnArray) { |
| 72 | + return $InputObject; |
| 73 | + } |
| 74 | + |
| 75 | + return $TRUE; |
| 76 | + } |
| 77 | + |
| 78 | + [bool]$IncludeFound = $FALSE; |
| 79 | + |
| 80 | + if ($ReturnArray) { |
| 81 | + # Handles if our input object is an array |
| 82 | + # Will return an array object instead of boolean |
| 83 | + foreach ($input in $InputObject) { |
| 84 | + if ((Test-IcingaArrayFilter -InputObject $input -Include $Include -Exclude $Exclude)) { |
| 85 | + $FilteredArray += $input; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return $FilteredArray; |
| 90 | + } else { |
| 91 | + foreach ($entry in $Exclude) { |
| 92 | + if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) { |
| 93 | + return $FALSE; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if ($Include.Count -eq 0) { |
| 98 | + return $TRUE; |
| 99 | + } |
| 100 | + |
| 101 | + foreach ($entry in $Include) { |
| 102 | + if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) { |
| 103 | + $IncludeFound = $TRUE; |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return $IncludeFound; |
| 109 | + } |
| 110 | + |
| 111 | + return $IncludeFound; |
| 112 | +} |
0 commit comments