Skip to content

Instantly share code, notes, and snippets.

@thewellington
Created April 27, 2015 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewellington/5196f76ee6a23b61387c to your computer and use it in GitHub Desktop.
Save thewellington/5196f76ee6a23b61387c to your computer and use it in GitHub Desktop.
JAMF Casper Suite change computer name before binding to AD
#!/bin/bash
#
# change_computer_name.sh
# 2015-04-06 by bill@wellingtonnet.net
#
# This script is for use with JAMF Casper Suite and makes some assumptions about
# the context in which it is running, specifically that arguments are served by
# the JSS and that arguments 1 through 3 are predefined as mount point, computer
# name, and username, respectively.
#
# It will change the ComputerName, LocalHostName, and HostName of a Macintosh
# computer to be a valid and unique name for the purposes of binding to Active
# Directory. It will then trigger the appropriate jamf policy event.
#
# let's set some variables
TOKEN="" # api token from user with api access.
BASEURL=""
JAMFEVENT="bindactivedirectory" # this is a custom event that gets called after the renaming process
mkdir -p /tmp/jamf/
XMLFILE=/tmp/jamf/location.xml
# get location data from jss
curl -sSk -H "Accept: application/xml" -u api:$TOKEN $BASEURL/JSSResource/computers/match/`system_profiler SPHardwareDataType | grep Serial | awk '{print $NF}'` > $XMLFILE
USERNAME="$(echo "cat /computers/computer/username/text()" | xmllint --nocdata --shell ${XMLFILE} | sed '1d;$d')"
# build appropriate computer name
TRUNCUSERNAME=`printf $USERNAME | cut -c 1-10`
MACADDR=`ifconfig en0 | grep ether | awk '{print $2}' | awk 'BEGIN {FS=":"};{print $5$6}'`
COMPUTERNAME="${TRUNCUSERNAME}-${MACADDR}"
NETBIOSNAME=`echo $COMPUTERNAME | tr '[:upper:]' '[:lower:]'`
# ok, let's get to work
if [[ $NETBIOSNAME =~ [a-z]+-[0-9a-f]* ]]; then
sudo ntpdate -u time.apple.com
sudo scutil --set ComputerName $NETBIOSNAME
sudo scutil --set LocalHostName $NETBIOSNAME
sudo scutil --set HostName $NETBIOSNAME
sudo jamf policy -event $JAMFEVENT -verbose
echo "Success: Computer name: ${NETBIOSNAME}"
exit
else
echo "Error: Invalid computer name: ${NETBIOSNAME}"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment