2021-08-06 12:12:27 -04:00
|
|
|
function Set-IcingaServiceUser()
|
2019-10-17 16:51:12 -04:00
|
|
|
{
|
2021-07-16 15:38:08 -04:00
|
|
|
param (
|
2024-03-14 12:16:09 -04:00
|
|
|
[string]$User = 'NT Authority\NetworkService',
|
2019-10-31 08:41:42 -04:00
|
|
|
[securestring]$Password,
|
2019-11-03 12:01:54 -05:00
|
|
|
[string]$Service = 'icinga2',
|
2024-03-14 12:16:09 -04:00
|
|
|
[switch]$SetPermission = $FALSE
|
2019-10-17 16:51:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ([string]::IsNullOrEmpty($User)) {
|
2022-01-28 02:56:29 -05:00
|
|
|
Write-IcingaConsoleError -Message 'Please specify a username to modify the service user';
|
2019-10-17 16:51:12 -04:00
|
|
|
return $FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 12:16:09 -04:00
|
|
|
switch ($Service.ToLower()) {
|
|
|
|
|
'icinga2' {
|
|
|
|
|
if ($Global:Icinga.Protected.Environment.'Icinga Service'.Present -eq $FALSE) {
|
|
|
|
|
Write-IcingaConsoleDebug -Message 'Trying to update user for service "icinga2" while the service is not installed yet';
|
|
|
|
|
return $FALSE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
};
|
|
|
|
|
'icingapowershell' {
|
|
|
|
|
if ($Global:Icinga.Protected.Environment.'PowerShell Service'.Present -eq $FALSE) {
|
|
|
|
|
Write-IcingaConsoleDebug -Message 'Trying to update user for service "icingapowershell" while the service is not installed yet';
|
|
|
|
|
return $FALSE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
};
|
|
|
|
|
default {
|
|
|
|
|
if ($null -eq (Get-Service $Service -ErrorAction SilentlyContinue)) {
|
|
|
|
|
return $FALSE;
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-08-06 12:12:27 -04:00
|
|
|
}
|
|
|
|
|
|
2023-05-26 08:17:26 -04:00
|
|
|
if ($User.Contains('@')) {
|
|
|
|
|
$UserData = $User.Split('@');
|
|
|
|
|
$User = [string]::Format('{0}\{1}', $UserData[1], $UserData[0]);
|
|
|
|
|
} elseif ($User.Contains('\') -eq $FALSE) {
|
2019-10-28 12:07:48 -04:00
|
|
|
$User = [string]::Format('.\{0}', $User);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 09:18:13 -05:00
|
|
|
$ArgString = 'config {0} obj= "{1}" password= "{2}"';
|
2020-08-04 08:48:32 -04:00
|
|
|
if ($null -eq $Password) {
|
2019-10-31 08:41:42 -04:00
|
|
|
$ArgString = 'config {0} obj= "{1}"{2}';
|
2019-10-17 16:51:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Output = Start-IcingaProcess `
|
|
|
|
|
-Executable 'sc.exe' `
|
2019-10-31 08:41:42 -04:00
|
|
|
-Arguments ([string]::Format($ArgString, $Service, $User, (ConvertFrom-IcingaSecureString $Password))) `
|
2019-10-17 16:51:12 -04:00
|
|
|
-FlushNewLines $TRUE;
|
|
|
|
|
|
|
|
|
|
if ($Output.ExitCode -eq 0) {
|
2019-11-03 12:01:54 -05:00
|
|
|
|
2024-03-14 12:16:09 -04:00
|
|
|
switch ($Service.ToLower()) {
|
|
|
|
|
'icinga2' {
|
|
|
|
|
$Global:Icinga.Protected.Environment.'Icinga Service'.User = $User;
|
|
|
|
|
break;
|
|
|
|
|
};
|
|
|
|
|
'icingapowershell' {
|
|
|
|
|
$Global:Icinga.Protected.Environment.'PowerShell Service'.User = $User;
|
|
|
|
|
break;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-03 12:01:54 -05:00
|
|
|
if ($SetPermission) {
|
2021-08-06 12:12:27 -04:00
|
|
|
Set-IcingaAgentServicePermission | Out-Null;
|
2024-03-14 12:16:09 -04:00
|
|
|
Set-IcingaUserPermissions -IcingaUser $User;
|
2019-11-03 12:01:54 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-06 12:12:27 -04:00
|
|
|
Write-IcingaConsoleNotice 'Service User "{0}" for service "{1}" successfully updated' -Objects $User, $Service;
|
2019-10-17 16:51:12 -04:00
|
|
|
return $TRUE;
|
|
|
|
|
} else {
|
2020-05-13 10:53:15 -04:00
|
|
|
Write-IcingaConsoleError ([string]::Format('Failed to update the service user: {0}', $Output.Message));
|
2019-10-17 16:51:12 -04:00
|
|
|
return $FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-06 12:12:27 -04:00
|
|
|
|
|
|
|
|
Set-Alias -Name 'Set-IcingaAgentServiceUser' -Value 'Set-IcingaServiceUser';
|