From 57e55ecada8821da41d86c0537661578f684bbbe Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Tue, 27 Jul 2021 14:56:37 +0200 Subject: [PATCH] Fix exception on negative values --- doc/31-Changelog.md | 6 +++++- lib/core/tools/Convert-IcingaPluginThresholds.psm1 | 14 +++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index cdeda23..e09a191 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -11,7 +11,11 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic [Issue and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/15?closed=1) -## Enhancements +### Bugfixes + +* [#311](https://github.com/Icinga/icinga-powershell-framework/issues/311) Fixes an issue with negative inputs on some scenarios which will cause an exception for checks instead of continuing executing them properly + +### Enhancements * [#301](https://github.com/Icinga/icinga-powershell-framework/pull/301) Improves error handling to no longer print passwords in case `String` is used for `SecureString` arguments * [#305](https://github.com/Icinga/icinga-powershell-framework/pull/305) Adds a new Cmdlet to test if functions with `Add-Type` are already present inside the current scope of the shell diff --git a/lib/core/tools/Convert-IcingaPluginThresholds.psm1 b/lib/core/tools/Convert-IcingaPluginThresholds.psm1 index e97b475..7930fc4 100644 --- a/lib/core/tools/Convert-IcingaPluginThresholds.psm1 +++ b/lib/core/tools/Convert-IcingaPluginThresholds.psm1 @@ -103,6 +103,7 @@ function Convert-IcingaPluginThresholds() [bool]$HasTilde = $FALSE; [bool]$HasAt = $FALSE; + [bool]$Negate = $FALSE; $Value = ''; $WorkUnit = ''; @@ -114,6 +115,11 @@ function Convert-IcingaPluginThresholds() $ThresholdValue = $ThresholdValue.Replace('@', ''); } + if ($ThresholdValue[0] -eq '-' -And $ThresholdValue.Length -ge 1) { + $Negate = $TRUE; + $ThresholdValue = $ThresholdValue.Substring(1, $ThresholdValue.Length - 1); + } + If (($ThresholdValue -Match "(^[\d\.]*) ?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)")) { $WorkUnit = 'B'; if ([string]::IsNullOrEmpty($RetValue.Unit) -eq $FALSE -And $RetValue.Unit -ne $WorkUnit) { @@ -133,7 +139,7 @@ function Convert-IcingaPluginThresholds() $Value = ([string]$ThresholdValue).Replace(' ', '').Replace('%', ''); $RetValue.Unit = $WorkUnit; } else { - # Load all other units/values genericly + # Load all other units/values generically [string]$StrNumeric = ''; [bool]$FirstChar = $TRUE; [bool]$Delimiter = $FALSE; @@ -161,6 +167,12 @@ function Convert-IcingaPluginThresholds() } } + if ((Test-Numeric $Value) -And $Negate) { + $Value = $Value * -1; + } elseif ($Negate) { + $Value = [string]::Format('-{0}', $Value); + } + if ($HasTilde) { $ConvertedValue += [string]::Format('~{0}', $Value); } elseif ($HasAt) {