Renaming Macs in AD Environment Using Self Service

Cem
Valued Contributor

* I have changed the subject to make it easy to find from archive*
Here is the script to change the name with ease in Casper Self Service (MCX applied to disabled Sharing prefs Naming). Thanks guys for all the help with osascript.
I have tested both scripts successfully in my environment. I suggest test it before using in your environment.

1 REPLY 1

Cem
Valued Contributor

hanges their Mac names)

  1. First script will remove the Mac from AD (set as before in policy)

#!/bin/sh
# This script will remove existing Directory records and will reset Search Paths
# to /Active Directory/mycompany.com
# Tested with 10.6.x
# Remove all files containing AD bindings informations

#####################################
#Clock restart / Remove existing settings
#####################################

#Restart ntpdate
StartService ()
{
if [ "${TIMESYNC:-YES-}" "-YES-" ] &&
! GetPID ntpd > /dev/null; then

CheckForNetwork

if [ -f /var/run/NetworkTime.StartupItem -o "${NETWORKUP}" "-NO-" ]; th
en exit; fi
touch /var/run/NetworkTime.StartupItem

echo =93Starting network time synchronization=94

# Synchronize our clock to the network=92s time,
# then fire off ntpd to keep the clock in sync.
ntpdate -bvs
ntpd -f /var/run/ntp.drift -p /var/run/ntpd.pid
fi
}

# Remove exisiting
logger =93Removing existing DS Config=94

if [ ! -d "/Library/Preferences/DirectoryService/ActiveDirectory" ]; then
rm -R /Library/Preferences/DirectoryService/ActiveDirectory*
fi

if [ ! -d "/Library/Preferences/DirectoryService/DSLDAPv3PlugInConfig" ]; t
hen
rm -R /Library/Preferences/DirectoryService/DSLDAPv3PlugInConfig*
fi

if [ ! -d "/Library/Preferences/DirectoryService/SearchNode" ]; then
rm -R /Library/Preferences/DirectoryService/SearchNode*
fi

if [ ! -d "/Library/Preferences/DirectoryService/ContactsNode" ]; then
rm -R /Library/Preferences/DirectoryService/ContactsNode*
fi

if [ ! -d "/Library/Preferences/edu.mit.Kerberos" ]; then
rm -R /Library/Preferences/edu.mit.Kerberos
fi

# Restart DirectoryService (necessary to reload AD plugin activation settin
gs)
killall DirectoryService
/bin/sleep 3

exit 0

  1. Second Script will rename the Mac and the record .txt file. (set as "bef ore" and make sure it runs after the first script above - alternatively you can combined them)

#!/bin/bash

# Lets enter the computername with GUI
getname()
{
asmsg`arch -i386 /usr/bin/osascript << EOT
tell app "System Events"
Activate
set foo to text returned of (display dialog "Enter the new name of the Comp
uter" buttons {"OK"} default answer "Type Computer Name Here")
end tell
EOT`
if [ "$asmsg" "false" ]; then
getname
else
echo $asmsg
fi
}
computernamegetname

# Set the computername
networksetup -setcomputername ${computername}

# This will update the original hostname record and it will keep the old re
cord with date stamp then set the permissions
mv /Library/Receipts/cname.txt /Library/Receipts/oldcname_`date +%M-%H-%m-%
d-%y`.txt
networksetup -getcomputername > /Library/Receipts/cname.txt
chmod -R 644 /Library/Receipts/cname.txt
chown -R root:wheel /Library/Receipts/cname.txt

# lets set the computer
setNamecat /Library/Receipts/cname.txt
scutil --set ComputerName ${setName}
scutil --set LocalHostName ${setName}

# Run Recon to update JSS record with the new name
jamf recon

exit 0 ## computermame and the record updated successfully
exit 1 ## fail

  1. Then check bind to AD in Account section of the Policy to bind the Mac t o AD. I have some editing Macs here that they don=92t need to be on domain. So I have set a pop up window message saying " Please run "AD bind" from S elf Service to join this Mac to domain. Then Restart or Shut Down."

I hope this is useful=85

Cem