2019-10-17 16:51:12 -04:00
|
|
|
function Set-IcingaAgentServiceUser()
|
|
|
|
|
{
|
2021-07-16 15:38:08 -04:00
|
|
|
param (
|
2019-10-17 16:51:12 -04:00
|
|
|
[string]$User,
|
2019-10-31 08:41:42 -04:00
|
|
|
[securestring]$Password,
|
2019-11-03 12:01:54 -05:00
|
|
|
[string]$Service = 'icinga2',
|
|
|
|
|
[switch]$SetPermission
|
2019-10-17 16:51:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ([string]::IsNullOrEmpty($User)) {
|
|
|
|
|
throw 'Please specify a username to modify the service user';
|
|
|
|
|
return $FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-28 12:07:48 -04:00
|
|
|
if ($User.Contains('\') -eq $FALSE) {
|
|
|
|
|
$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
|
|
|
|
|
|
|
|
if ($SetPermission) {
|
|
|
|
|
Set-IcingaUserPermissions;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-13 10:53:15 -04:00
|
|
|
Write-IcingaConsoleNotice 'Service User successfully updated'
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|