Moving Home Directories - Old User to New User - Same Domain?

dgreening
Valued Contributor II

Hey guys and gals,

A bit of background first:

  1. We need to move a few users from their existing AD user account to a brand new AD user account - the accounts will have different names/UIDs
  2. We will be on the same domain, so no binding will need to be done
  3. We will have the users log in with their new account and enable themselves for FV2 before any data gets moved

We want to script a way to provide a Self Service method (when logged in as the local admin account) to move users data from their old account into their new account. This will probably involve the following:

  1. Deleting the new user accounts user directory (rm -Rf /Users/newuser)
  2. Changing the name of the old home directory to the new user name (probably using mv)
  3. Changing the ownership of the renamed home directory to the new user account (chown -R newuser /Users/newuser)

Does anyone have a script designed to do this? I don't need a complete ready-to-go product, just some idea on how to accomplish this with minimal headache. Thanks in advance!

5 REPLIES 5

stevewood
Honored Contributor II
Honored Contributor II

@dgreening if you rename the existing home folder to the new AD user name and change permissions, then there's no need to delete the old or have the user login and then log out of the new account.

  1. Have user logout.
  2. Local admin logs in and opens Self Service.
  3. Self Service policy/script does:
mv /Users/$oldUserName /Users/$newUserName
chmod -R "$newUserName" /Users/$newUserName
  1. Local admin logs out
  2. User logs in with new account info.

That should be all you need to do.

stevewood
Honored Contributor II
Honored Contributor II

Thinking about it a little more, how are you going to get the new account info? And are you really going to have an administrator go around to each user's machine to do this?

dgreening
Valued Contributor II

Fortunately we are only talking about IT folks moving their own accounts. I am going to use CocoaDialog to prompt for the old user name and then the new user name for use in the script. We do have numbers in some of our usernames, so I need to figure out how to work around that in CocoaDialog.

stevewood
Honored Contributor II
Honored Contributor II

@dgreening I realized you mentioned FV also. If your local admin user is enabled for FV already, you do not have to have them login to enable themselves for FV either. You can script it as part of the home folder move. I used that method when I had to change AD domains last year. I utilized CocoaDialog to grab the user's password and feed that into a plist file that FV used to re-add the user to FV. You can find my script here:

Move Domains

If you need more help scripting it, just let us know.

dgreening
Valued Contributor II

So I have the script which leverages CocoaDialog complete!

First off a BIG shout-out to Ross Derewianko who wrote the original CocoaDialog computer name changer script which this script is based on!

#!/bin/sh
########################################################################
# Created By: Daniel Greening, Sapient Corporation
# Creation Date: December, 2015 
# Last modified: December 18, 2015
# Brief Description: Changes User home from old account to new account
########################################################################

# Check for CocoaDialog and if not install it

if [ -d "/Library/Application Support/JAMF/bin/CocoaDialog.app" ]; then
    CoDi="/Library/Application Support/JAMF/bin/CocoaDialog.app/Contents/MacOS/cocoaDialog"
else
    echo "CocoaDialog.app not found installing" 
    jamf policy -event cocoa
    CoDi="/Library/Application Support/JAMF/bin/CocoaDialog.app/Contents/MacOS/cocoaDialog"
fi

########################################################################
# Functions
#######################################################################

# Cleans the first two characters out (cocoaDialog adds a 1 
 to the string value which we don't need.)

function cleannewname() {
    newname=${newname:2}
}

function cleanoldname() {
    oldname=${oldname:2}
}

# Prompts the user for their old username

function oldnameprompt() {
    oldname=`"$CoDi" standard-inputbox --float --title "Account Migration Utility" --informative-text "Enter your old AD username:"`

    if [ "$oldname" == "2" ]; then
        echo "user cancelled"
        exit 1
    fi
    cleanoldname
}   

# Prompts the user for their new username

function newnameprompt() {

    newname=`"$CoDi" standard-inputbox --float --title "Account Migration Utility" --informative-text "Enter your new AD username:"`

    if [ "$newname" == "2" ]; then
        echo "user cancelled"
        exit 1
    fi
    cleannewname
}

# Checks for a blank oldname, and if its blank prompt agian 

function checkforoldblank() {
    while [[ -z $oldname && {$oldname+1} ]]
    do
        oldnameprompt
    done
}

# Checks for blank newname, and if its blank prompt again

function checkfornewblank() {
    while [[ -z $newname && {$newname+1} ]]
    do
        newnameprompt
    done
}

# Makes the home directory changes

function homechanger() {
    rm -Rf /Users/"$newname"
    mv /Users/"$oldname"/ /Users/"$newname"
    2>/dev/null chown -R "$newname" /Users/"$newname"
}

########################################################################
# Script
########################################################################

oldnameprompt
checkforoldblank
newnameprompt
checkfornewblank
homechanger