Change from Local to Domain
#!/bin/bash
#### Variables
loggedInUser=`ls -l /dev/console | awk '{print $3}'`
allusers=`sudo dscl . list /Users | grep -v '^_' | xargs | sed -e 's/ /", "/g' | awk '{ print "\""$0"\""}'`
#####Change these Variables only
dirSearchBase="DC=risd,DC=net"
dirHost="'risd.net'"
domnusr="'domin accountt'"
dmnpass="'domain password'"
binduser="'domain join account'"
bindpass="'password'"
Domainname="'risd.net'"
oldusr=$loggedInUser
#oldusr="$(osascript -e 'tell application "System Events" to activate' -e 'tell application "System Events" to return (choose from list {'"$allusers"'} with prompt "Choose your Account" with title "Account" OK button name "Select" cancel button name "Quit")')"
adur=`dscl . -read /Users/$oldusr | grep OriginalAuthenticationAuthority`
if [ "$adur" != "" ]; then
osascript -e 'Tell application "System Events" to display dialog "This user is already a network User" buttons {"Cancel"} cancel button "Cancel" default button "Cancel" with icon caution'
exit 1
fi
######VErify User is same as AD USER
###This Does The Query for the CN of the User Account
#ldapsearch -h "risd.net" -p 389 -x -D "domjoin" -w "Tms-k12J01n" -b "DC=risd,DC=net" -s sub sAMAccountName="hhorton"
verifusr=`ldapsearch -h "risd.net" -p 389 -x -D "domain account" -w "domain password" -b "DC=risd,DC=net" -s sub sAMAccountName="$loggedInUser" | grep sAMAccountName | tail -1 | awk '{print $NF}' | tr '[:upper:]' '[:lower:]'`
if [ $verifusr == $loggedInUser ]; then
# The user pressed Cancel
# exit 1 # exit with an error status
#fi
###delete user but not Data
sudo dscl . -delete /Users/$oldusr
#######Fix Permissions and ownership of Files
/System/Library/CoreServices/ManagedClient.app/Contents/Resources/createmobileaccount -n $loggedInUser
sudo dscl . -append /Groups/admin GroupMembership $loggedInUser
sleep 5
chown -R $oldusr /Users/$oldusr &
osascript -e 'Tell application "System Events" to display dialog "Please log out, and log in with your network credentials." buttons {"OK"} cancel button "OK" default button "OK" with icon caution'
else
osascript -e 'Tell application "System Events" to display dialog "The user is not a network User. Please contact Helpdesk" buttons {"OK"} cancel button "OK" default button "OK" with icon caution'
fi