icinga-powershell-framework/lib/core/windows/Remove-IcingaWindowsUser.psm1

37 lines
1.3 KiB
PowerShell
Raw Normal View History

2021-08-06 12:12:27 -04:00
function Remove-IcingaWindowsUser()
{
param (
$IcingaUser = 'icinga'
);
$UserConfig = Get-IcingaWindowsUserConfig -UserName $IcingaUser;
2021-08-06 12:12:27 -04:00
if ($UserConfig.UserExist -eq $FALSE -Or $UserConfig.IcingaManagedUser -eq $FALSE) {
if ($UserConfig.UserExist -eq $FALSE) {
Write-IcingaConsoleNotice 'The user "{0}" is not present on this system' -Objects $IcingaUser;
} elseif ($UserConfig.IcingaManagedUser -eq $FALSE) {
Write-IcingaConsoleNotice 'The user "{0}" was not created by Icinga for Windows. Unable to remove user' -Objects $IcingaUser;
}
return @{
'User' = $IcingaUser;
'SID' = $SID;
};
2021-08-06 12:12:27 -04:00
}
$Result = Start-IcingaProcess -Executable 'net' -Arguments ([string]::Format('user "{0}" /DELETE', $UserConfig.Name));
2021-08-06 12:12:27 -04:00
if ($Result.ExitCode -ne 0) {
Write-IcingaConsoleError 'Failed to delete user "{0}": {1}' -Objects $IcingaUser, $Result.Error;
} else {
# Delete Home Directory
$HomePath = Join-Path -Path ($ENV:HOMEDRIVE) -ChildPath (Join-Path -Path '\Users\' -ChildPath $IcingaUser);
Remove-ItemSecure -Path $HomePath -Recurse -Force | Out-Null;
2021-08-06 12:12:27 -04:00
}
return @{
'User' = $UserConfig.Caption;
'SID' = $UserConfig.SID;
};
}