1#!/bin/bash
2
3#log the output of the script to the jamf.log for easy viewing
4logfile=/var/log/jamf.log;
5exec >> $logfile 2>&1;
6
7
8
9
10#------------------------------
11#-------BEGIN VARIABLES--------
12#------------------------------
13
14scriptname="populate_username.sh";
15breadcrumb="/Library/BCGS/breadcrumb_username_populated.txt";
16currentUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName` >/dev/null 2>/dev/null
17
18#set LoggedInUser as the current user
19if [ `ls -l /dev/console | cut -d " " -f 4` == "root" ]
20then
21 #script is run at login, so the user is the $3 variable
22 LoggedInUser=$3
23else
24 #script is run as self service, so the user is not sent to the script
25 LoggedInUser=`ls -l /dev/console | cut -d " " -f 4`
26fi
27
28#------------------------------
29#-------END VARIABLES----------
30#------------------------------
31
32
33
34echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: "
35echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: ------------------------------------------------------"
36echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: --- Starting $scriptname"
37echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: "
38echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: Script variables:"
39echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: $LoggedInUser = $LoggedInUser"
40echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: $currentUser = $currentUser"
41echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]:"
42
43if [ -f "$breadcrumb" ]
44then
45 # We should skip running this script, as it looks like it has already run at a previous reboot.
46 # In theory this should never occur, as it should be exlcuded in the policy, so this is a second
47 # measure.
48 echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: Skipped populating the username as we have already done this."
49else
50 # Grab the username of the user that last logged in (current user).
51 # This will only return an accurate username the second time the user logs in.
52 echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: Username is '$currentUser'."
53
54 if [ $LoggedInUser == $currentUser ]
55 then
56 # Usernames are the same, so lets proceed.
57 # Submit an inventory report and include the current user to be written to the
58 echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: Usernames are the same ($LoggedInUser, $currentUser)"
59 echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: Running 'jamf recon -endUsername $LoggedInUser."
60 jamf recon -endUsername $currentUser >/dev/null 2>/dev/null
61
62 # Create the breadcrumb so we know not to run the script again
63 echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: Creating the breadcrumb."
64 echo $LoggedInUser >> $breadcrumb
65
66 # Run recon again to pick up the breadcrumb extension attribute
67 echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: Running jamf recon for a second time."
68 jamf recon >/dev/null 2>/dev/null
69 else
70 # Do not write the breadcrumb, then the script will run next time
71 echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: Usernames are not the same ($LoggedInUser, $currentUser)"
72 fi
73fi
74
75
76echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: "
77echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: --- Finished $scriptname -----------------------------"
78echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: ------------------------------------------------------"
79echo "`date +"%a %b %d %X"` `hostname` jamf[script-$scriptname]: "
80
81exit 0