Copy File Scripts

esutedy
New Contributor II

Hi all,

Am trying to create a script to copy files from /Library/Shared/TeamsBackground to the User's Microsoft Teams folder.

As a test case, i deliberately do not have Backgrounds and Backgrounds/Uploads folders in the User's Teams directory. When i ran the script, it just went to the last Else statement "Teams Never Ran". I know for a fact that the Teams folder ~/Library/Application Support/Microsoft/Teams exist.

What am I missing?

Thanks

#!/bin/sh

CURRENTUSER=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0];  username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

if [[ -f "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads/file1.jpg" ]]
then
    echo "Already copied to the user's folder"
    exit 0
else
    if [[ -d "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads" ]]
    then
        echo "Upload folder exist"
        sudo "cp /Library/Shared/TeamsBackground/*" "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads"
        exit 0
    else    
        if [[ -d "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds" ]]
        then
            echo "Backgrounds folder exist"
            sudo mkdir /Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads
            sudo cp "/Library/Shared/TeamsBackground/*" "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads"
            exit 0
        else
            if [[ -d "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams" ]]
            then
                echo "Teams Folder Exist, dumping the files"
                sudo mkdir /Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds
                sudo mkdir /Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads
                sudo cp "/Library/Shared/TeamsBackground/*" "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads"
                exit 0
            else
                echo "Teams Never Ran"
                exit 0
            fi  
        fi
    fi
fi
exit 0
7 REPLIES 7

sdagley
Esteemed Contributor II

@esutedy Don't quote "${CURRENTUSER}" inside something that's already quoted, and don't escape spaces if the path is quoted. i.e. the first conditional should be:

if [[ -f "/Users/${CURRENTUSER}/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads/file1.jpg" ]]

(You have multiple incorrect uses of this pattern)

Don't use sudo in a script being run from Jamf as it's already running in the root context so this

sudo "cp /Library/Shared/TeamsBackground/*" "/Users/"${CURRENTUSER}"/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads"

should be

cp "/Library/Shared/TeamsBackground/*" "/Users/${CURRENTUSER}/Library/Application Support/Microsoft/Teams/Backgrounds/Uploads/"

Your mkdir commands need to ve followed by chown commands to assign the new folders to the user whose directory you're creating them in

Don't use the deprecated backtick (`) pattern to wrap a call you want to assign to a variable. i.e. this line

CURRENTUSER=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0];  username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

should be:

CURRENTUSER=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0];  username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')

esutedy
New Contributor II

Thanks @sdagley . worked like a charm.

Thank you for your chown advice as well. I didnt realise that the user themselves need full access to their own Teams folders...

sdagley
Esteemed Contributor II

@esutedy Good to hear you got it working. I should ahve also mentioned a couple of sites you'll find useful when it comes to tips on writing/troubleshooting scripts:

Scripting OS X - Definitely articles on what the name implies, plus lots of other Mac related info

ShellCheck - A tool for analyzing scripts that will quickly point out syntax errors, as well as semantic issues that aren't so obvious

tc_pmg
New Contributor II

@esutedy glad this worked out for you. I'm using this as a reference to do something similar but for a different app's set of user folders. Would you mind sharing the script? I'm getting "python: command not found" in the logs. Plus i'm not sure where and what to add the CHOWN command that @sdagley mentioned.

 

thank you in advanced!

sdagley
Esteemed Contributor II

@tc_pmg Python is no longer a default install in macOS, so a different mechanism for finding the current user is needed. See this post on Scripting OS X for one of those: https://scriptingosx.com/2020/02/getting-the-current-user-in-macos-update/

Do a 'man chown' in Terminal to see what the command parameters are. You need to use that command to assign ownership of the files/folders otherwise the user may not have access to them after they're copied.

SamirCH
New Contributor

i m really intersted by thi sscript cause i looking for a way to pusgh Teams Background sloothly, and when i use Composer, the results do not match correctly

tc_pmg
New Contributor II

I was able to accomplish what I wanted. I'm sure there are other ways to do this but it was the fastest and least complicated way for me.

  • Composer > drag the folder/files that need to appear in a user's folder (for example: ~/Desktop)
  • Save as DMG
  • After adding it to Jamf Pro, set the options for FUT/FEU.
  • Create the policy with that packaged DMG.