mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 15:19:58 -05:00
41 lines
1.1 KiB
PowerShell
41 lines
1.1 KiB
PowerShell
function Set-IcingaAgentServiceUser()
|
|
{
|
|
param(
|
|
[string]$User,
|
|
[securestring]$Password,
|
|
[string]$Service = 'icinga2',
|
|
[switch]$SetPermission
|
|
);
|
|
|
|
if ([string]::IsNullOrEmpty($User)) {
|
|
throw 'Please specify a username to modify the service user';
|
|
return $FALSE;
|
|
}
|
|
|
|
if ($User.Contains('\') -eq $FALSE) {
|
|
$User = [string]::Format('.\{0}', $User);
|
|
}
|
|
|
|
$ArgString = 'config {0} obj= "{1}" password="{2}"';
|
|
if($null -eq $Password) {
|
|
$ArgString = 'config {0} obj= "{1}"{2}';
|
|
}
|
|
|
|
$Output = Start-IcingaProcess `
|
|
-Executable 'sc.exe' `
|
|
-Arguments ([string]::Format($ArgString, $Service, $User, (ConvertFrom-IcingaSecureString $Password))) `
|
|
-FlushNewLines $TRUE;
|
|
|
|
if ($Output.ExitCode -eq 0) {
|
|
|
|
if ($SetPermission) {
|
|
Set-IcingaUserPermissions;
|
|
}
|
|
|
|
Write-Host 'Service User successfully updated'
|
|
return $TRUE;
|
|
} else {
|
|
Write-Host ([string]::Format('Failed to update the service user: {0}', $Output.Message));
|
|
return $FALSE;
|
|
}
|
|
}
|