Skip to main content
Question

Copy File Scripts


Forum|alt.badge.img+3

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

sdagley
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3536 replies
  • June 1, 2020

@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 + "
");')

Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 2 replies
  • June 1, 2020

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
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3536 replies
  • June 1, 2020

@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


Forum|alt.badge.img+6
  • Contributor
  • 11 replies
  • January 19, 2023

@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
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3536 replies
  • January 19, 2023
tc_pmg wrote:

@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!


@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
Forum|alt.badge.img
  • Jamf Heroes
  • 1 reply
  • February 9, 2023

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


Forum|alt.badge.img+6
  • Contributor
  • 11 replies
  • February 10, 2023

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.

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings