forked from jmahlman/Mac-Admin-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename-Machine-by-User.sh
67 lines (58 loc) · 2.03 KB
/
rename-Machine-by-User.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/sh
#
#
# Created by John Mahlman, University of the Arts Philadelphia ([email protected])
# Name: rename-Machine-by-User.sh
#
# Purpose: Will rename a machine based on the assigned user in the JSS via API.
# Use this during DEP enrollment to avoid the "admin's MacBook" naming
#
# Changelog
# 2/15/18: - Initial script creation
#
# Get the JSS URL from the Mac's jamf plist file
if [ -e "/Library/Preferences/com.jamfsoftware.jamf.plist" ]; then
jssURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url)
else
echo "No JSS server set. Exiting..."
exit 1
fi
# Define API username and password information & JSS Group name from passed parameters
if [ ! -z "$4" ]; then
apiUser="$4"
else
echo "No value passed to $4 for api username. Exiting..."
exit 1
fi
if [ ! -z "$5" ]; then
apiPass="$5"
else
echo "No value passed to $5 for api password. Exiting..."
exit 1
fi
SERIAL=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
USERINFO=$(curl -k ${jssURL}JSSResource/computers/serialnumber/${SERIAL}/subset/location -H "Accept: application/xml" --user "${apiUser}:${apiPass}")
USERNAME=$(echo $USERINFO | /usr/bin/awk -F'<username>|</username>' '{print $2}' | tr [A-Z] [a-z])
DEVICEINFO=$(curl -k ${jssURL}JSSResource/computers/serialnumber/${SERIAL}/subset/hardware -H "Accept: application/xml" --user "${apiUser}:${apiPass}")
MODEL=$(echo $DEVICEINFO | /usr/bin/awk -F'<model_identifier>|</model_identifier>' '{print $2}')
if [ -z "$USERNAME" ]
then
USERNAME="$SERIAL"
fi
if echo "$MODEL" | grep -q "MacBookAir"
then
PREFIX="MBA"
elif echo "$MODEL" | grep -q "MacBookPro"
then
PREFIX="MBP"
else
echo "No model identifier found."
PREFIX=""
fi
COMPUTERNAME="${USERNAME}-${PREFIX}"
COMPUTERNAME=`echo ${COMPUTERNAME:0:15}`
echo "Setting computer name to $COMPUTERNAME"
/usr/sbin/scutil --set ComputerName "$COMPUTERNAME"
/usr/sbin/scutil --set LocalHostName "$COMPUTERNAME"
/usr/sbin/scutil --set HostName "$COMPUTERNAME"
dscacheutil -flushcache