Posted on 03-04-2020 03:33 PM
Hi Everyone,
I have a policy that runs a script which grabs a users full name, splits it into 2 (first and last) and puts a . in between and then appends the domain name. The issue is that when the policy runs off of a trigger, it grabs "System" as the first name and "Administrator" as the last name. However, when I run the policy manually by running "sudo jamf policy" in terminal, it grabs the correct first and last name and the username is then inputted correctly.
#!/bin/sh
# Get the logged in users First and Last name
firstName=$(dscl . -read "/Users/$(who am i | awk '{print $1}')" RealName | sed -n 's/^ //g;2p' | cut -d' ' -f1)
lastName=$(dscl . -read "/Users/$(who am i | awk '{print $1}')" RealName | sed -n 's/^ //g;2p' | cut -d' ' -f2)
domain="@domainname.com"
userEmail=$firstName.$lastName$domain
echo "$userEmail"
#example userEmail: first.last@domainname.com
# Run recon, submitting the users username which as of 8.61+ can then perform an LDAP lookup
sudo jamf recon -endUsername $userEmail
exit 0
Solved! Go to Solution.
Posted on 03-04-2020 03:39 PM
You need to get the current logged in user and use that instead of the Who Am I bit you have, when the policy is run via the jamf binary via a recon or checking it runs as root and doesnt use sudo at all.
A modified script that should work would be as follows
#bin/sh
# Get the logged in users First and Last name
loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
firstName=$(dscl . -read "/Users/$loggedInUser" RealName | sed -n 's/^ //g;2p' | cut -d' ' -f1)
lastName=$(dscl . -read "/Users/$loggedInUser" RealName | sed -n 's/^ //g;2p' | cut -d' ' -f2)
domain="@domainname.com"
userEmail=$firstName.$lastName$domain
echo "$userEmail"
#example userEmail: first.last@domainname.com
# Run recon, submitting the users username which as of 8.61+ can then perform an LDAP lookup
sudo jamf recon -endUsername $userEmail
exit 0
Posted on 03-04-2020 03:37 PM
Try getting the logged in users info the more Apple approved way:
https://macmule.com/2014/11/19/how-to-get-the-currently-logged-in-user-in-a-more-apple-approved-way/
Posted on 03-04-2020 03:39 PM
You need to get the current logged in user and use that instead of the Who Am I bit you have, when the policy is run via the jamf binary via a recon or checking it runs as root and doesnt use sudo at all.
A modified script that should work would be as follows
#bin/sh
# Get the logged in users First and Last name
loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
firstName=$(dscl . -read "/Users/$loggedInUser" RealName | sed -n 's/^ //g;2p' | cut -d' ' -f1)
lastName=$(dscl . -read "/Users/$loggedInUser" RealName | sed -n 's/^ //g;2p' | cut -d' ' -f2)
domain="@domainname.com"
userEmail=$firstName.$lastName$domain
echo "$userEmail"
#example userEmail: first.last@domainname.com
# Run recon, submitting the users username which as of 8.61+ can then perform an LDAP lookup
sudo jamf recon -endUsername $userEmail
exit 0
Posted on 03-10-2020 10:05 AM
@drtaru THANK YOU SO MUCH