diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index 7385664..0f88674 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -17,6 +17,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic ### Bugfixes +* [#781](https://github.com/Icinga/icinga-powershell-framework/issues/781) Fixes Icinga for Windows being stuck during installation while fetching service information over CIM-Instances, if other services are frozen, blocking the CIM-Request * [#784](https://github.com/Icinga/icinga-powershell-framework/issues/784) Fixes Icinga for Windows threshold comparison which wrongly compared warning/critical thresholds for non-range values (like Match) * [#785](https://github.com/Icinga/icinga-powershell-framework/issues/785) Fixes Icinga for Windows freezing during loading in case the `config.json` is empty * [#786](https://github.com/Icinga/icinga-powershell-framework/issues/786) Fixes Icinga for Windows installer to always force the installation of the service, to ensure it is present diff --git a/lib/core/tools/Get-IcingaServices.psm1 b/lib/core/tools/Get-IcingaServices.psm1 index f68888d..4d4e274 100644 --- a/lib/core/tools/Get-IcingaServices.psm1 +++ b/lib/core/tools/Get-IcingaServices.psm1 @@ -1,30 +1,66 @@ function Get-IcingaServices() { param ( - [array]$Service, + [array]$Service = @(), [array]$Exclude = @() ); $ServiceInformation = Get-Service; $ServiceWmiInfo = $null; + $ServiceFilter = New-Object System.Text.StringBuilder; - if ($Service.Count -eq 0) { - $ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service; - } else { - try { - $ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service | - ForEach-Object { - foreach ($svc in $Service) { - if ($_.Name -Like $svc) { - return $_; - } - } - } | Select-Object StartName, Name, ExitCode, StartMode, PathName; - } catch { - Exit-IcingaThrowException -InputString $_.Exception.Message -StringPattern 'wildcard' -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.RegexError; - Exit-IcingaThrowException -CustomMessage $_.Exception.Message -ExceptionType 'Input' -ExceptionThrown $_.Exception.Message; - return $null; + if ($Service.Count -gt 0) { + $ServiceFilter.Append('(') | Out-Null; + + foreach ($svc in $Service) { + if ($ServiceFilter.Length -gt 1) { + $ServiceFilter.Append(' OR ') | Out-Null; + } + + $ServiceFilter.Append( + [string]::Format( + 'Name LIKE "{0}"', + $svc.Replace('*', '%') + ) + ) | Out-Null; } + + $ServiceFilter.Append(')') | Out-Null; + } + + if ($Exclude.Count -gt 0) { + if ($ServiceFilter.Length -gt 0) { + $ServiceFilter.Append(' AND (') | Out-Null; + } else { + $ServiceFilter.Append('(') | Out-Null; + } + + [bool]$First = $TRUE; + + foreach ($svc in $Exclude) { + if ($First -eq $FALSE) { + $ServiceFilter.Append(' AND ') | Out-Null; + } + + $First = $FALSE; + + $ServiceFilter.Append( + [string]::Format( + 'NOT Name LIKE "{0}"', + $svc.Replace('*', '%') + ) + ) | Out-Null; + } + + $ServiceFilter.Append(')') | Out-Null; + } + + try { + $ServiceWmiInfo = Get-IcingaWindowsInformation -ClassName Win32_Service -Filter $ServiceFilter.ToString() | Select-Object StartName, Name, ExitCode, StartMode, PathName; + } catch { + Exit-IcingaThrowException -InputString $_.Exception.Message -StringPattern 'wildcard' -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.RegexError; + Exit-IcingaThrowException -CustomMessage $_.Exception.Message -ExceptionType 'Input' -ExceptionThrown $_.Exception.Message; + return $null; } if ($null -eq $ServiceInformation) {