Created
October 11, 2018 20:32
-
-
Save scottd3v/b6fbd54764993c2ad19f72f4826f09e4 to your computer and use it in GitHub Desktop.
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 | |
| # Ensures that script is run as ROOT | |
| if [[ "${UID}" != 0 ]]; then | |
| (echo >&2 "Error: $0 script must be run as root") | |
| exit 1 | |
| fi | |
| # Ensures that the system is not domain bound | |
| readonly domainBoundCheck=$(dsconfigad -show) | |
| if [[ "${domainBoundCheck}" ]]; then | |
| (echo >&2 "Cannot run on domain bound system. Unbind system and try again.") | |
| exit 1 | |
| fi | |
| oldUser=$1 | |
| # Test to ensure logged in user is not being renamed | |
| readonly loggedInUser=$(ls -la /dev/console | cut -d " " -f 4) | |
| if [[ "${loggedInUser}" == "${oldUser}" ]]; then | |
| echo "Cannot rename active GUI logged in user. Log in with another admin account and try again." | |
| exit 1 | |
| fi | |
| # Query existing user accounts | |
| readonly existingUsers=($(dscl . -list /Users | grep -Ev "^_|com.*|root|nobody|daemon|\/" | cut -d, -f1 | sed 's|CN=||g')) | |
| # Ensure old user account is correct and account exists on system | |
| if [[ ! " ${existingUsers[@]} " =~ " ${oldUser} " ]]; then | |
| echo "${oldUser} account not present on system to update" | |
| exit 1 | |
| fi | |
| # Ensure new user account is not already in use | |
| if [[ " ${existingUsers[@]} " =~ " ${newUser} " ]]; then | |
| echo "${newUser} account already present on system. Cannot add duplicate" | |
| exit 1 | |
| fi | |
| # Echo existing usernames | |
| echo "Existing users:" ${existingUsers} | |
| # Query existing home folders | |
| readonly existingHomeFolders=($(ls /Users)) | |
| # Ensure existing home folder is not in use | |
| if [[ " ${existingHomeFolders[@]} " =~ " ${newUser} " ]]; then | |
| echo "${newUser} home folder already in use on system. Cannot add duplicate" | |
| exit 1 | |
| fi | |
| # Checks if user is logged in | |
| loginCheck=$(ps -Ajc | grep ${oldUser} | grep loginwindow | awk '{print $2}') | |
| # Logs out user if they are logged in | |
| timeoutCounter='0' | |
| while [[ "${loginCheck}" ]]; do | |
| echo "${oldUser} account logged in. Logging user off to complete username update." | |
| sudo launchctl bootout gui/$(id -u ${oldUser}) | |
| Sleep 5 | |
| loginCheck=$(ps -Ajc | grep ${oldUser} | grep loginwindow | awk '{print $2}') | |
| timeoutCounter=$((${timeoutCounter} + 1)) | |
| if [[ ${timeoutCounter} -eq 4 ]]; then | |
| echo "Timeout unable to log out ${oldUser} account." | |
| exit 1 | |
| fi | |
| done | |
| # Captures current "RealName" this is the displayName | |
| fullRealName=$(dscl . -read /Users/${oldUser} RealName) | |
| # Formats "RealName" | |
| readonly origRealName=$(echo ${fullRealName} | cut -d' ' -f2-) | |
| echo "current RealName: " ${origRealName} | |
| # Captures current NFS home directory | |
| readonly origHomeDir=$(dscl . -read "/Users/${oldUser}" NFSHomeDirectory | awk '{print $2}' -) | |
| echo "current NFS home dir: " ${origHomeDir} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment