Created
August 22, 2014 04:12
-
-
Save bryanzak/8723a440b5390d41df20 to your computer and use it in GitHub Desktop.
This is a script we have used for a while to rename an Active Directory user account that is cached locally. When a teacher has a name change, their AD short name changes, but not the user ID. OS X doesn't like this. So this script is run by our field techs from an admin account to simply rename the account from old to new. The teacher can then …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# AccountRenamer.command | |
# | |
# Interactive tool to rename an account from one name to another (like when a teacher gets renamed and their e account changes) | |
# | |
# HISTORY: | |
# | |
# 1.0 2013-03-25 bmp - initial release | |
release="1.0 (March 25, 2013)" | |
current_user=`id -un` | |
oldacct="none" | |
newacct="none" | |
set_defalt_oldacct() | |
{ | |
for user in `ls /Users/`; do | |
user_type=${user:0:1} | |
if [ "$user_type" == "e" ]; then | |
oldacct=$user | |
fi | |
done | |
} | |
check_admin() | |
{ | |
result=`dsmemberutil checkmembership -U $current_user -G admin | grep -c "not"` | |
if [ $result == 1 ]; then | |
clear | |
echo | |
echo "**** Current user ($current_user) is NOT admin" | |
echo "**** This script requires that the current user has admin privs" | |
echo | |
echo "Exiting script." | |
exit 1 | |
fi | |
} | |
# return result: 0=no errors, 1=source and dest are the same, warning! | |
check_replace_mode_folders() | |
{ | |
if [ "$newacct" == "none" ]; then | |
echo | |
echo "**** You have not yet specified a new account name." | |
echo "**** Use the Configure option to do so." | |
echo | |
echo | |
read -p "Press Return to continue" | |
return 1 | |
fi | |
if [ "$newacct" == "$current_user" ]; then | |
echo | |
echo "**** You are logged into the “new” account ($current_user) used for renaming" | |
echo "**** You must be logged into a third account, typically Field Support" | |
echo | |
echo | |
read -p "Press Return to continue" | |
return 1 | |
fi | |
if [ "$oldacct" == "$current_user" ]; then | |
echo | |
echo "**** You are logged into the “old” account you are renaming" | |
echo "**** You must be logged into a third account, typically Field Support" | |
echo | |
echo | |
read -p "Press Return to continue" | |
return 1 | |
fi | |
if [ "$oldacct" == "$newacct" ]; then | |
echo | |
echo "**** New and Old accounts are the same name. This will fail." | |
echo "**** You must rename from one account (“eoldname”) to another (“enewname”)" | |
echo | |
echo | |
read -p "Press Return to continue" | |
return 1 | |
fi | |
if [ ! -d "/Users/$oldacct" ]; then | |
echo | |
echo "**** OLD account ($oldacct) does not exist" | |
echo "**** Please verify the name of the account to rename" | |
echo | |
echo | |
read -p "Press Return to continue" | |
return 1 | |
fi | |
if [ -d "/Users/$newacct" ]; then | |
echo | |
echo "**** NEW account ($newacct) already exists!" | |
echo "**** The new account name must not exist. Correct this before proceeding" | |
echo "**** (possibly by deleting the new account in System Preferences > Accounts)" | |
echo | |
echo | |
read -p "Press Return to continue" | |
return 1 | |
fi | |
return 0 | |
} | |
change_oldacct() | |
{ | |
echo | |
echo "Change the SHORT name of the OLD network account. Should be all lowercase" | |
echo | |
echo "Examples: esmithj123" | |
echo | |
echo "Enter: " | |
read choice | |
oldacct=`echo "$choice" | awk '{print tolower($0)}'` | |
} | |
change_newacct() | |
{ | |
echo | |
echo "Change the SHORT name of the NEW network account. Should be all lowercase" | |
echo | |
echo "Examples: esmithj123" | |
echo | |
echo "Enter: " | |
read choice | |
newacct=`echo "$choice" | awk '{print tolower($0)}'` | |
} | |
# returns 0 if no error, 1 if failure | |
do_rename() | |
{ | |
check_replace_mode_folders | |
if [ $? == 1 ]; then | |
return 1 | |
fi | |
echo "*************" | |
echo "************* Password for \"$current_user\" will be needed here" | |
echo "*************" | |
sudo echo | |
cd "/Users" | |
echo "Unlocking contents of $oldacct" | |
sudo chflags -R nouchg $oldacct # unlock all the source files | |
sudo chmod -RN $oldacct # remove ACLs from old account folder | |
echo "Replacing $newacct with $oldacct" | |
# sudo rm -Rf $newacct # delete the new account's home folder | |
sudo mv $oldacct $newacct # rename old account folder as new account | |
sudo rm -f "/Users/$newacct/Library/Keychains/login.keychain" | |
echo "Deleting $oldacct user account now that it is an empty shell" | |
sudo dscl . delete /Users/$oldacct 2> /dev/null | |
echo "Rename Complete, login with $newacct to complete the process" | |
return 0 | |
} | |
do_configure() | |
{ | |
clear | |
echo | |
echo "Change the mode of the script or the names used by the script:" | |
echo | |
echo "N - New account name: \""$newacct"\"" | |
echo | |
echo "O - Old account name: \""$oldacct"\"" | |
echo | |
echo "R - Return to Main Menu" | |
echo | |
printf "Enter choice: " | |
read choice | |
echo | |
case "`echo "z$choice" | awk '{print tolower(substr($0,2))}'`" in | |
n) | |
change_newacct | |
do_change_names | |
;; | |
o) | |
change_oldacct | |
do_change_names | |
;; | |
r) | |
;; | |
*) # Show choices again | |
do_change_names | |
;; | |
esac | |
} | |
do_main_menu() | |
{ | |
clear | |
echo | |
echo "Account Renamer $release" | |
echo "“Renames” an account from one to another" | |
echo | |
echo "R - Rename Account (old=$oldacct to new=$newacct)" | |
echo | |
echo "C - Configure Account Names" | |
echo | |
echo "Q - Quit." | |
echo | |
echo | |
printf "Enter choice: " | |
read choice | |
echo | |
case "`echo "z$choice" | awk '{print tolower(substr($0,2))}'`" in | |
r) | |
do_rename | |
if [ $? == 1 ]; then | |
do_main_menu | |
fi | |
;; | |
c) | |
do_configure | |
do_main_menu | |
;; | |
q) | |
echo "Quitting." | |
exit 0 | |
;; | |
*) # Show choices again | |
do_main_menu | |
;; | |
esac | |
} | |
check_admin | |
set_defalt_oldacct | |
do_main_menu |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment