2020-05-22 03:52:48 -04:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Wrapper for Restart-Service which catches errors and prints proper output messages
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Restarts a service if it is installed and prints console messages if a restart
|
|
|
|
|
was triggered or the service is not installed
|
|
|
|
|
.FUNCTIONALITY
|
|
|
|
|
Wrapper for restart service which catches errors and prints proper output messages
|
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS>Restart-IcingaService -Service 'icinga2';
|
|
|
|
|
.PARAMETER Service
|
|
|
|
|
The name of the service to be restarted
|
|
|
|
|
.INPUTS
|
|
|
|
|
System.String
|
|
|
|
|
.OUTPUTS
|
|
|
|
|
Null
|
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/Icinga/icinga-powershell-framework
|
|
|
|
|
#>
|
|
|
|
|
|
2019-10-31 09:32:19 -04:00
|
|
|
function Restart-IcingaService()
|
|
|
|
|
{
|
2022-02-15 09:14:27 -05:00
|
|
|
param (
|
2019-10-31 09:32:19 -04:00
|
|
|
$Service
|
|
|
|
|
);
|
|
|
|
|
|
2020-05-30 08:42:31 -04:00
|
|
|
if (Get-Service "$Service" -ErrorAction SilentlyContinue) {
|
2020-05-13 10:53:15 -04:00
|
|
|
Write-IcingaConsoleNotice ([string]::Format('Restarting service "{0}"', $Service));
|
2020-05-30 08:42:31 -04:00
|
|
|
|
2022-02-15 09:14:27 -05:00
|
|
|
Invoke-IcingaCommand -ArgumentList $Service -ScriptBlock {
|
|
|
|
|
try {
|
|
|
|
|
Restart-Service "$($IcingaShellArgs[0])" -ErrorAction Stop;
|
|
|
|
|
Start-Sleep -Seconds 2;
|
|
|
|
|
Optimize-IcingaForWindowsMemory;
|
|
|
|
|
} catch {
|
|
|
|
|
Write-IcingaConsoleError -Message 'Failed to restart service "{0}". Error: {1}' -Objects $IcingaShellArgs[0], $_.Exception.Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-22 03:52:48 -04:00
|
|
|
} else {
|
|
|
|
|
Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service;
|
2019-10-31 09:32:19 -04:00
|
|
|
}
|
2022-02-15 09:14:27 -05:00
|
|
|
|
|
|
|
|
Optimize-IcingaForWindowsMemory;
|
2019-10-31 09:32:19 -04:00
|
|
|
}
|