I have a new use case: I need to delete an existing local domain user account on the computer. To do this, the user must be logged out.
If the user is logged in, they must be asked to log out so that the deletion process can begin.
The request works, the user is logged out by the script, but the script does not continue running afterwards.
If another command is used instead of Logout.exe, e.g., Notepad.exe, the script would at least continue to run until a message appears stating that the user profile cannot be deleted because it is in use.
It is therefore clear that it depends on the Logoff.exe command. What could be the reason for this?
A similar challenge seems to have arisen before (see link) – without a solution.
I have tried the following, but I have not achieved any result.
PRE-INSTALLATION:
$oReturn = [System.Windows.Forms.MessageBox]::Show("In order for your user profile to be repaired, you must log out of your workstation. Please click OK to log out.","Logoff Required",[System.Windows.Forms.MessageBoxButtons]::OKCancel)
if($oReturn -eq 'OK'){
Write-log -Message "The user logged off."
Execute-ProcessAsUser -Path “$($envWinDir)\System32\logoff.exe” -RunLevel LeastPrivilege -Wait -PassThru
#Execute-ProcessAsUser -Path “$($envWinDir)\System32\shutdown.exe” -Parameters '/l' -RunLevel LeastPrivilege
}
if($oReturn -eq 'Cancel'){
[System.Windows.Forms.MessageBox]::Show("You have chosen not to log out.","Logoff Canceled",[System.Windows.Forms.MessageBoxButtons]::OK)
Write-log -Message "Logoff was canceled by the user"
Exit-Script -ExitCode 1234
}
INSTALLATION:
[string[]]$ProfilePaths = (Get-UserProfiles -ExcludeDefaultUser | Select-Object -ExpandProperty 'NTAccount').Trim(’Domainname\’)
ForEach ($Profile in $ProfilePaths) {
Write-log -Message "An attempt was made to delete the user profile of $($Profile)."
Remove-Folder -Path "$($envSystemDrive)\Users\$($Profile)"
}
[string[]]$ProfileList = Get-UserProfiles -ExcludeDefaultUser | Select-Object -ExpandProperty 'SID'
ForEach ($Profile in $ProfileList) {
Write-log -Message "An attempt was made to delete the SID $($Profile) in the registry."
Remove-RegistryKey -Key "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$($Profile)"
}
POST-INSTALLATION:
Show-InstallationRestartPrompt -NoSilentRestart $false -SilentCountdownSeconds 00
#Execute-Process -Path “$($envWinDir)\System32\cmd.exe” -Parameters "/c shutdown.exe /r /f /t 00" -RunLevel LeastPrivilege -PassThru