|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Clears the entire content of the Icinga Agent API directory located |
| 4 | + at Program Data\icinga2\var\lib\icinga2\api\ |
| 5 | +.DESCRIPTION |
| 6 | + Clears the entire content of the Icinga Agent API directory located |
| 7 | + at Program Data\icinga2\var\lib\icinga2\api\ |
| 8 | +.FUNCTIONALITY |
| 9 | + Clears the entire content of the Icinga Agent API directory located |
| 10 | + at Program Data\icinga2\var\lib\icinga2\api\ |
| 11 | +.EXAMPLE |
| 12 | + PS>Clear-IcingaAgentApiDirectory; |
| 13 | +.EXAMPLE |
| 14 | + PS>Clear-IcingaAgentApiDirectory -Force; |
| 15 | +.PARAMETER Force |
| 16 | + In case the Icinga Agent service is running while executing the command, |
| 17 | + the force argument will ensure the service is stopped before the API |
| 18 | + directory is flushed and restarted afterwards |
| 19 | +.INPUTS |
| 20 | + System.Boolean |
| 21 | +.LINK |
| 22 | + https://github.com/Icinga/icinga-powershell-framework |
| 23 | +#> |
| 24 | + |
| 25 | +function Clear-IcingaAgentApiDirectory() |
| 26 | +{ |
| 27 | + param ( |
| 28 | + [switch]$Force = $FALSE |
| 29 | + ); |
| 30 | + |
| 31 | + $IcingaService = (Get-IcingaServices -Service icinga2).icinga2; |
| 32 | + $ApiDirectory = (Join-Path -Path $Env:ProgramData -ChildPath 'icinga2\var\lib\icinga2\api\'); |
| 33 | + |
| 34 | + if ((Test-Path $ApiDirectory) -eq $FALSE) { |
| 35 | + Write-IcingaConsoleError 'The Icinga Agent API directory is not present on this system. Please check if the Icinga Agent is installed'; |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + if ($IcingaService.configuration.Status.raw -eq 4 -And $Force -eq $FALSE) { |
| 40 | + Write-IcingaConsoleError 'The API directory can not be deleted while the Icinga Agent is running. Use the "-Force" argument to stop the service, flush the directory and restart the service again.'; |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if ($IcingaService.configuration.Status.raw -eq 4) { |
| 45 | + Stop-IcingaService icinga2; |
| 46 | + Start-Sleep -Seconds 1; |
| 47 | + } |
| 48 | + |
| 49 | + Write-IcingaConsoleNotice 'Flushing Icinga Agent API directory'; |
| 50 | + Remove-ItemSecure -Path (Join-Path -Path $ApiDirectory -ChildPath '*') -Recurse -Force | Out-Null; |
| 51 | + Start-Sleep -Seconds 1; |
| 52 | + |
| 53 | + if ($IcingaService.configuration.Status.raw -eq 4) { |
| 54 | + Start-IcingaService icinga2; |
| 55 | + } |
| 56 | +} |
0 commit comments