Posting this for anyone experiencing an Office 365 login loop (Usually happens with Outlook). If reverting to legacy Outlook doesn't resolve your issue, run the keychain removal script below. It will restart the device. (The script is sourced from Scott Feit on the MacAdmins Slack channel and has resolved the issue multiple times for me)
#!/bin/bash
# Define the target directory
TARGET_DIR="$HOME/Library/Keychains"
# Function to log messages
log_message() {
echo "$(date +"%Y-%m-%d %H:%M:%S") - $1"
}
# Close the Keychain Access app
log_message "Closing Keychain Access application."
pkill -x "Keychain Access"
# Check if the target directory exists
if [ -d "$TARGET_DIR" ]; then
log_message "Starting cleanup of directories in $TARGET_DIR"
success_count=0
fail_count=0
# Find all directories within the target directory and delete them, logging each action
find "$TARGET_DIR" -type d -mindepth 1 -print0 | while IFS= read -r -d $'\\0' dir; do
rm -rf "$dir"
if [ $? -eq 0 ]; then
log_message "Successfully deleted directory: $dir"
((success_count++))
else
log_message "Failed to delete directory: $dir"
((fail_count++))
fi
done
if [ $fail_count -eq 0 ]; then
log_message "Cleanup completed successfully. Running 'sudo jamf recon'."
# Run jamf recon
sudo jamf recon
log_message "Initiating forced restart with 15-second delay."
# Display a popup message with a 15-second timer before the restart
osascript -e 'tell app "System Events" to display dialog "Your computer will restart in 15 seconds." buttons {"OK"} default button 1 giving up after 15'
# Wait for 15 seconds before the restart
sleep 15
# Force a restart of the computer
sudo shutdown -r now "Script-initiated forced restart."
else
log_message "Cleanup completed with $fail_count failures. Restart aborted."
fi
else
log_message "Target directory does not exist. No actions were taken."
fi