Script to copy files to each users profile (domain users not local)

chuck_fleury
New Contributor

I am looking for a script that can copy a file to each user's profile on the system.
Does anyone have one that they can share?
The problem is the FEU only applies to local user and not domain users so any custom settings that need to be deployed have to be copied after the installation of the software.
Any help would be awesome.

4 REPLIES 4

mikeh
Contributor II

What follows is a snippet from a script that I used recently to search for and remove a particular file from each user's folder. I've made it a bit more generic, so you'll need to adapt it for your specific needs. Hopefully it will get you started.

The logic of the FOR loop selection criteria is: get the long-listing of the /Users folder, extract those lines that start with a d, indicating a directory, remove the Shared folder from the list, and then extract the final field (/Users/username) from the line. You then use that variable in the FOR loop to target each user.

#!/bin/bash
    LOGIT="logger -t $(basename $0) "
    # get full path to each user's folder (skipping Shared)
    for USER in $(ls -ld /Users/* | grep ^d | grep -v "Shared" | awk {'print $NF'})
    do
        # perform a condition check - don't overwrite a file if it already exists, check for a file, whatever
        if [ some condition ]
        then
            /do/some/action $USER/dir/file
            $LOGIT $USER - some message to be logged
        else
            $LOGIT $USER - some message to be logged
        fi
    done

Be careful when using Casper to add or modify a file with a script like this. Since Casper runs the script as root, the file the script creates will get the permissions of root, not the user; if root modified the script, it may also get owned by root. Not good from the user's standpoint. You can get around this by using the command 'sudo -u $(basename $USER) /do/some/action $USER/dir/file' when attempting to perform the desired action. You might also want to check the permission of the created/modified file while you're at it.

I'm sure that this is not ideal - for instance, I'm assuming that there are no spaces in the directory names, and that a user hasn't stashed a folder of their own in that folder. The expert scripters on this forum will no doubt have better suggestions (and I look forward to seeing them, too).

acdesigntech
Contributor II

This is what you are looking for:

#!/bin/sh

## This script will copy anything inside the /A/U/ISD/Inst/User Settings folder to each existing user directory and the user template
for PREF_ITEM in /Applications/Utilities/ISD/Installs/User Settings/*; 
do
    for USER_HOME in /Users/*;
    do
        ## Get just the short name for each user
        USER_ID=`basename "${USER_HOME}"`
        ## Get just the item name that you want to copy (no paths)
        PREF_ITEM_COMMON_NAME=`basename "${PREF_ITEM}"`

        ##Don't bother copying to Shared, there's no point
        if [ "$USER_ID" != "Shared" ]; then
            echo "Now Copying to $USER_ID"
            rsync -avE --ignore-errors "${PREF_ITEM}" ${USER_HOME}
            ## Set permissions accordingly once the item has been copied
            chown -R "$USER_ID" "${USER_HOME}"
            chmod -R 700 /Users/"$USER_ID"/AG Applications/
            chmod -R 700 /Users/"$USER_ID"/Desktop/
            chmod -R 700 /Users/"$USER_ID"/Documents/
            chmod -R 700 /Users/"$USER_ID"/Downloads/
            chmod -R 700 /Users/"$USER_ID"/Library/
            chmod -R 700 /Users/"$USER_ID"/Movies/
            chmod -R 700 /Users/"$USER_ID"/Music/
            chmod -R 700 /Users/"$USER_ID"/Pictures/
            chmod 755 /Users/"$USER_ID"/Public/
            chmod -R 733 /Users/"$USER_ID"/Public/Drop Box/
            chmod 755 /Users/"$USER_ID"/Sites/
            chmod 644 /Users/"$USER_ID"/Sites/*
            chmod -R 755 /Users/"$USER_ID"/Sites/images/
            echo "----DONE-----"
            echo ""
        fi
    done
    echo "Now copying to user template..."
    ## Make sure to also copy the item to the user template, and change permissions accordingly
    rsync -avE --ignore-errors "${PREF_ITEM}" /System/Library/User Template/English.lproj/
    chown -R root /System/Library/User Template/English.lproj/"${PREF_ITEM_COMMON_NAME}"
    chmod -R 700 /System/Library/User Template/English.lproj/"${PREF_ITEM_COMMON_NAME}"
done

## Clean up when you're done
rm -r /Applications/Utilities/ISD/Installs/User Settings/*

chuck_fleury
New Contributor

Thanks for the quick response.
I will try both of these today.

jski
New Contributor

These are great, much appreciated