2020-05-22 03:52:48 -04:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Wrapper for Start-Service which catches errors and prints proper output messages
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Starts a service if it is installed and prints console messages if a start
|
|
|
|
|
was triggered or the service is not installed
|
|
|
|
|
.FUNCTIONALITY
|
|
|
|
|
Wrapper for Start-Service which catches errors and prints proper output messages
|
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS>Start-IcingaService -Service 'icinga2';
|
|
|
|
|
.PARAMETER Service
|
|
|
|
|
The name of the service to be started
|
|
|
|
|
.INPUTS
|
|
|
|
|
System.String
|
|
|
|
|
.OUTPUTS
|
|
|
|
|
Null
|
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/Icinga/icinga-powershell-framework
|
|
|
|
|
#>
|
|
|
|
|
|
2019-10-31 09:32:19 -04:00
|
|
|
function Start-IcingaService()
|
|
|
|
|
{
|
|
|
|
|
param(
|
|
|
|
|
$Service
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (Get-Service $Service -ErrorAction SilentlyContinue) {
|
2020-05-22 03:52:48 -04:00
|
|
|
Write-IcingaConsoleNotice -Message 'Starting service "{0}"' -Objects $Service;
|
2020-05-30 08:42:31 -04:00
|
|
|
|
2022-04-27 06:43:55 -04:00
|
|
|
& powershell.exe -Command {
|
|
|
|
|
$Service = $args[0];
|
2022-02-15 09:14:27 -05:00
|
|
|
try {
|
2022-04-27 06:43:55 -04:00
|
|
|
Start-Service "$Service" -ErrorAction Stop;
|
2022-02-15 09:14:27 -05:00
|
|
|
Start-Sleep -Seconds 2;
|
|
|
|
|
Optimize-IcingaForWindowsMemory;
|
|
|
|
|
} catch {
|
2022-04-27 06:43:55 -04:00
|
|
|
Write-IcingaConsoleError -Message 'Failed to start service "{0}". Error: {1}' -Objects $Service, $_.Exception.Message;
|
2022-02-15 09:14:27 -05:00
|
|
|
}
|
2022-04-27 06:43:55 -04:00
|
|
|
} -Args $Service;
|
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
|
|
|
}
|