|
1 | 1 | function Get-IcingaServices() |
2 | 2 | { |
3 | 3 | param ( |
4 | | - [array]$Service, |
| 4 | + [array]$Service = @(), |
5 | 5 | [array]$Exclude = @() |
6 | 6 | ); |
7 | 7 |
|
8 | 8 | $ServiceInformation = Get-Service; |
9 | 9 | $ServiceWmiInfo = $null; |
| 10 | + $ServiceFilter = New-Object System.Text.StringBuilder; |
10 | 11 |
|
11 | | - if ($Service.Count -eq 0) { |
12 | | - $ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service; |
13 | | - } else { |
14 | | - try { |
15 | | - $ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service | |
16 | | - ForEach-Object { |
17 | | - foreach ($svc in $Service) { |
18 | | - if ($_.Name -Like $svc) { |
19 | | - return $_; |
20 | | - } |
21 | | - } |
22 | | - } | Select-Object StartName, Name, ExitCode, StartMode, PathName; |
23 | | - } catch { |
24 | | - Exit-IcingaThrowException -InputString $_.Exception.Message -StringPattern 'wildcard' -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.RegexError; |
25 | | - Exit-IcingaThrowException -CustomMessage $_.Exception.Message -ExceptionType 'Input' -ExceptionThrown $_.Exception.Message; |
26 | | - return $null; |
| 12 | + if ($Service.Count -gt 0) { |
| 13 | + $ServiceFilter.Append('(') | Out-Null; |
| 14 | + |
| 15 | + foreach ($svc in $Service) { |
| 16 | + if ($ServiceFilter.Length -gt 1) { |
| 17 | + $ServiceFilter.Append(' OR ') | Out-Null; |
| 18 | + } |
| 19 | + |
| 20 | + $ServiceFilter.Append( |
| 21 | + [string]::Format( |
| 22 | + 'Name LIKE "{0}"', |
| 23 | + $svc.Replace('*', '%') |
| 24 | + ) |
| 25 | + ) | Out-Null; |
| 26 | + } |
| 27 | + |
| 28 | + $ServiceFilter.Append(')') | Out-Null; |
| 29 | + } |
| 30 | + |
| 31 | + if ($Exclude.Count -gt 0) { |
| 32 | + if ($ServiceFilter.Length -gt 0) { |
| 33 | + $ServiceFilter.Append(' AND (') | Out-Null; |
| 34 | + } else { |
| 35 | + $ServiceFilter.Append('(') | Out-Null; |
| 36 | + } |
| 37 | + |
| 38 | + [bool]$First = $TRUE; |
| 39 | + |
| 40 | + foreach ($svc in $Exclude) { |
| 41 | + if ($First -eq $FALSE) { |
| 42 | + $ServiceFilter.Append(' AND ') | Out-Null; |
| 43 | + } |
| 44 | + |
| 45 | + $First = $FALSE; |
| 46 | + |
| 47 | + $ServiceFilter.Append( |
| 48 | + [string]::Format( |
| 49 | + 'NOT Name LIKE "{0}"', |
| 50 | + $svc.Replace('*', '%') |
| 51 | + ) |
| 52 | + ) | Out-Null; |
27 | 53 | } |
| 54 | + |
| 55 | + $ServiceFilter.Append(')') | Out-Null; |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + $ServiceWmiInfo = Get-IcingaWindowsInformation -ClassName Win32_Service -Filter $ServiceFilter.ToString() | Select-Object StartName, Name, ExitCode, StartMode, PathName; |
| 60 | + } catch { |
| 61 | + Exit-IcingaThrowException -InputString $_.Exception.Message -StringPattern 'wildcard' -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.RegexError; |
| 62 | + Exit-IcingaThrowException -CustomMessage $_.Exception.Message -ExceptionType 'Input' -ExceptionThrown $_.Exception.Message; |
| 63 | + return $null; |
28 | 64 | } |
29 | 65 |
|
30 | 66 | if ($null -eq $ServiceInformation) { |
|
0 commit comments