Skip to content

Instantly share code, notes, and snippets.

@jworkmanjc
Created January 25, 2021 17:40
Show Gist options
  • Save jworkmanjc/1b3cbf6792b0a0ab4c906a2c9e0f5bc0 to your computer and use it in GitHub Desktop.
Save jworkmanjc/1b3cbf6792b0a0ab4c906a2c9e0f5bc0 to your computer and use it in GitHub Desktop.
Rename A User's RealName Attribute if it exists from firstname to lastname
#!/bin/bash
################################################################################
# This script will pull the provisionerID from the JumpCloud console and rename
# the specified user account to the matching username in JumpCloud. This script
# will not rename or remap the user's home directory.
################################################################################
# Variables
# User Account to Rename
UserToRename='firstname'
# Rename Account to:
AccountRename='firstname.lastname'
################################################################################
# Get Local Accounts on system and see if UserToRename exists
################################################################################
# Get the localAccounts on the system
localAccounts=$(dscl . list /Users UniqueID | awk '$2>500{print $1}' | grep -v super.admin | grep -v _jumpcloudserviceaccount)
if [[ ${localAccounts[*]} =~ $UserToRename ]]; then
echo "Matched $UserToRename user found"
selectedUser=$UserToRename
else
echo "Trying case insensitive search..."
selectedUser=$( echo "$localAccounts" | grep -i "\b$UserToRename\b")
fi
# If selectedUser is Null, exit
if [[ $selectedUser == "" ]]; then
echo "Could not find user exiting... the following users exist:"
echo "$localAccounts"
exit 1
else
echo "$selectedUser will be renamed"
fi
################################################################################
# Finally attempt to change the and realname attributes
################################################################################
dscl . -change /Users/${selectedUser} RecordName "${selectedUser}" "${AccountRename}"
testCommand=$(echo $?)
if [[ $testCommand != 0 ]]; then
echo "Failed to make username changes"
exit 1
else
echo "User's Username: $selectedUser changed to $AccountRename"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment