public
Last active

Updating a user's picture from a script

  • Download Gist
userpic.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/bin/bash
declare -xr awk="/usr/bin/awk"
declare -xr sw_vers="/usr/bin/sw_vers"
declare -xr dsimport="/usr/bin/dsimport"
declare -xr id="/usr/bin/id"
 
export UserName="$1"
export UserPicture="/path/to/$UserName.jpg"
 
export OsVersion=`$sw_vers -productVersion | $awk -F"." '{print $2;exit}'`
# Add the LDAP picture to the user record if dsimport is avaiable 10.6+
if [ -f "$UserPicture" ] ; then
# On 10.6 and higher this works
if [ "$OsVersion" -ge "6" ] ; then
declare -x Mappings='0x0A 0x5C 0x3A 0x2C'
declare -x Attributes='dsRecTypeStandard:Users 2 dsAttrTypeStandard:RecordName externalbinary:dsAttrTypeStandard:JPEGPhoto'
declare -x PictureImport="/Library/Caches/$UserName.picture.dsimport"
printf "%s %s \n%s:%s" "$Mappings" "$Attributes" "$UserName" "$UserPicture" >"$PictureImport"
# Check to see if the username is correct and import picture
if $id "$UserName" &>/dev/null ; then
# No credentials passed as we are running as root
$dsimport -g "$PictureImport" /Local/Default M &&
echo "Successfully imported users picture."
fi
fi
fi

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.