Skip to content

Instantly share code, notes, and snippets.

@gjpalau
Last active May 25, 2020 22:54
Show Gist options
  • Save gjpalau/92e802d422923762311c to your computer and use it in GitHub Desktop.
Save gjpalau/92e802d422923762311c to your computer and use it in GitHub Desktop.
mac renamer + AD bind script
#!/bin/bash
# uniMIS renamer script -- use with ComputerName name below
# By Gilbert Palau, Enterprise OS X - www.enterpriseosx.com
#
# this script renames the mac by chassi type + serial number: ??-SERIAL
#
# Legend:
#
laptop="ML-"
workstation="MW-"
server="S-"
tld="your.network.com"
# grabbing mac serial number...
serial=$(ioreg -l |grep "IOPlatformSerialNumber"|cut -d ""="" -f 2|sed -e s/[^[:alnum:]]//g)
# and model name...
model=$(ioreg -l |grep "product-name" |cut -d ""="" -f 2|sed -e s/[^[:alnum:]]//g | sed s/[0-9]//g)
# renaming based on model ie. prefix-computerserial.tld
case "$model" in
"MacBookPro" )
/usr/sbin/scutil --set ComputerName "$laptop$serial"
/usr/sbin/scutil --set LocalHostName "$laptop$serial"
/usr/sbin/scutil --set HostName "${laptop}${serial}.${tld}"
echo "$model"
echo "$laptop$serial"
echo "${laptop}${serial}.${tld}"
;;
"MacBookAir" )
/usr/sbin/scutil --set ComputerName "$laptop$serial"
/usr/sbin/scutil --set LocalHostName "$laptop$serial"
/usr/sbin/scutil --set HostName "${laptop}${serial}.${tld}"
echo "$model"
echo "$laptop$serial"
echo "${laptop}${serial}.${tld}"
;;
"MacPro" )
/usr/sbin/scutil --set ComputerName "$workstation$serial"
/usr/sbin/scutil --set LocalHostName "$workstation$serial"
/usr/sbin/scutil --set HostName "${workstation}${serial}.${tld}"
echo "$model"
echo "$workstation$serial"
echo "${workstation}${serial}.${tld}"
;;
"iMac" )
/usr/sbin/scutil --set ComputerName "$workstation$serial"
/usr/sbin/scutil --set LocalHostName "$workstation$serial"
/usr/sbin/scutil --set HostName "${workstation}${serial}.${tld}"
echo "$model"
echo "$workstation$serial"
echo "${workstation}${serial}.${tld}"
;;
"Macmini" )
/usr/sbin/scutil --set ComputerName "$workstation$serial"
/usr/sbin/scutil --set LocalHostName "$workstation$serial"
/usr/sbin/scutil --set HostName "${workstation}${serial}.${tld}"
echo "$model"
echo "$workstation$serial"
echo "${workstation}${serial}.${tld}"
;;
"Xserve" )
/usr/sbin/scutil --set ComputerName "$server$serial"
/usr/sbin/scutil --set LocalHostName "$server$serial"
/usr/sbin/scutil --set HostName "${server}${serial}.${tld}"
echo "$model"
echo "$server$serial"
echo "${server}${serial}.${tld}"
;;
* )
echo "Computer model not found."
exit 0
;;
esac
## Use if you want to use ComputerName name or if using the renamer
computerid=`/usr/sbin/scutil --get ComputerName`
## Use if you want bind account to be serial number
#computerid=$serial
# grab contents of System Preferences -> Sharing -> Computer Settings -> Info 1
# this will eventually contain building's OU so we can bind and place machine into
# the proper OU automatically.
location=`defaults read /Library/Preferences/com.apple.RemoteDesktop Text1`
uname=$2
pass=$3
##### This should be your old domain username. Using new ones here for testing
olduname=$2
oldpass=$3
ou="OU=NAME-OF-OU,DC=your,DC=network,DC=com"
os=`sw_vers -productVersion | awk -F "." '{print $2}'`
os=$(($os+0))
### Combine once you know what location will equal in AD OU
#newou="OU=$location,"$ou
### Use for testing in ESC
# newou="OU=ESC,"$ou
# production version; currently just drops machines into MacComputers OU
newou="$ou";
echo "****Current Serial Number****"
echo $serial
echo "****Current Name Number****"
echo $computerid
echo "****Current Location****"
echo $location
echo "--------"
echo "****Machine will be bound to following OU****"
echo $newou
# binding variables
$uname="ADUSERNAME"
$pass="ADPASSWORD"
if [[ "$os" -ge "7" ]] ; then
echo "higher than 10.6"
#10.7, 10.8, 10.9
echo "****Removing Current Bindings****"
dsconfigad -remove -username $olduname -password $oldpass
echo "Begin Binding..."
dsconfigad -force -add "your.network.com" -alldomains enable -mobile enable -mobileconfirm disable -computer $computerid -username $uname -password $pass -domain "DC=your,DC=network,DC=com" -ou "$newou"
else
echo "10.6.8 or lower"
#10.6
echo "****Removing Current Bindings****"
dsconfigad -r -u $olduname -p $oldpass
echo "Begin Binding..."
dsconfigad -f -a $computerid -domain myips.local -u $uname -p $pass -ou "$newou" -mobile enable -mobileconfirm disable -alldomains enable -groups "VDIStaff"
fi
## Adding search paths
sleep 20
## Create the search paths in DS for authentication and contacts.
dscl /Search -create / SearchPolicy CSPSearchPath
dscl /Search/Contacts -create / SearchPolicy CSPSearchPath
## Add our AD domain to the search paths.
dscl /Search/Contacts -append / CSPSearchPath "Active Directory/All Domains"
dscl /Search -append / CSPSearchPath "Active Directory/All Domains"
echo "Binding Complete"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment