icinga-powershell-framework/lib/core/windows/Uninstall-IcingaServiceUser.psm1

36 lines
1.2 KiB
PowerShell
Raw Normal View History

2021-08-06 12:12:27 -04:00
function Uninstall-IcingaServiceUser()
{
param (
$IcingaUser = 'icinga'
);
if ([string]::IsNullOrEmpty($IcingaUser)) {
Write-IcingaConsoleError 'The provided user cannot be empty.';
return;
}
Write-IcingaConsoleNotice 'Uninstalling user "{0}"' -Objects $IcingaUser;
# Fetch the current service user and SID
$ServiceUser = Get-IcingaServiceUser;
$ServiceUserSID = Get-IcingaUserSID $ServiceUser;
2021-08-06 12:12:27 -04:00
Stop-IcingaService 'icinga2';
Stop-IcingaForWindows;
2021-08-06 12:12:27 -04:00
Set-IcingaPowerShellConfig -Path 'Framework.Icinga.ServiceUser' -Value '';
Set-IcingaServiceUser -User 'NT Authority\NetworkService' -Service 'icinga2' | Out-Null;
Set-IcingaServiceUser -User 'NT Authority\NetworkService' -Service 'icingapowershell' | Out-Null;
Set-IcingaUserPermissions -IcingaUser $IcingaUser -Remove;
Update-IcingaWindowsUserPermission -SID $ServiceUserSID -Remove;
2021-08-06 12:12:27 -04:00
Remove-IcingaWindowsUser -IcingaUser $IcingaUser | Out-Null;
2021-08-06 12:12:27 -04:00
Restart-IcingaService 'icinga2';
Restart-IcingaForWindows;
2021-08-06 12:12:27 -04:00
Write-IcingaConsoleNotice 'User "{0}" including permissions was removed from this host' -Objects $IcingaUser;
}