So i've been tasked at work to write a script that reads a txt file from a server share and makes the specified users local admin on our OS X boxes.
The file itself consists of a string of entries such as this:-
The problem is that it doesn't work! Oh the loops all work and it does make sure things are present but ... the damn dscl command (and the dseditgroup -o edit -a $username -t user admin one before it) fail to work. dscl doesn't return any messages. The dseditgroup command returns a "record not found" message.
The really nasty part is that either command run on it's own actually works. The users in question are AD network users.
The file itself consists of a string of entries such as this:-
Code:
**computer name*:ADD:Administrator:domain\username
**computer name*:ADD:Administrator:domain\username,domain\username2
Code:
#!/bin/bash
# Set up needed variables here
macname=$( scutil --get ComputerName )
adminfile="/var/tmp/localadmins.txt"
OIFS=$IFS
IFS=$'\n'
# Mount fileshare where local admin file is kept.
mkdir /Volumes/SYSVOL
mount -o nobrowse -r -t smbfs //**domain controller**/SYSVOL /Volumes/SYSVOL
# Copy localadmin file to temp folder
cp /Volumes/SYSVOL/**path to file**/localadmins.txt /var/tmp/localadmins.txt
# Unmount and clean up fileshare.
diskutil umount force /Volumes/SYSVOL
# Read admin rights file line by line and process.
cat $adminfile | while read line
do
# Grab the name of the computer from the current line of the file
compname=$( echo "${line}" | cut -d : -f 1 )
# Find out how many users are listed by counting the commas
usercount=$( echo $((`echo ${line} | sed 's/[^,]//g' | wc -m`-1)) )
# Does the current computer name match the one in the file?
if [ $macname = $compname ];
then
# Loop to check name(s) are present on the mac and process them into the admin group.
for (( loop=0; loop<=usercount; loop++ ))
do
field=$(($loop + 1))
username=$( echo "${line}" | cut -d : -f 4 | cut -d "," -f ${field} | cut -c10- )
# Does specified user exist on the system in /Users? Loop round and if so place in admin group.
for Account in `ls /Users`
do
if [[ $Account == $username ]];
then
dscl . -merge /Groups/admin GroupMembership "$username"
fi
done
done
fi
done
# Clean up and finish
IFS=$OIFS
rm /var/tmp/localadmins.txt
exit 0
The problem is that it doesn't work! Oh the loops all work and it does make sure things are present but ... the damn dscl command (and the dseditgroup -o edit -a $username -t user admin one before it) fail to work. dscl doesn't return any messages. The dseditgroup command returns a "record not found" message.
The really nasty part is that either command run on it's own actually works. The users in question are AD network users.