Script to Replace UserName with EA "Last User Logged In"

jttavares
New Contributor III

Hi I am looking for a script to replace the Username in the inventory section with the Extension Attribute of "Last User Logged In".  In addition to add "@companyX.com" as a suffix to the user name.  This would help me greatly in getting Asset inventory for my company via Azure.  I am getting the Last User Logged In by Extension Attribute.  So if the "Last User Logged In" is john.doe , take john.doe and add @companyX.com and fill the Username in the inventory section as john.doe@companyX.com.  This would then poll Azure and I would get all the data I need.  Still looking for a way to not manually update the username field when deploying a Mac to another user.  My scripting skills are limited, apologies. Thanks,

JT

1 ACCEPTED SOLUTION

robjschroeder
New Contributor III
#!/bin/bash

# Define domain suffix to add to username
domain="companyX.com"

# Get the last user of computer
lastUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`

if [ $lastUser != "" ]; then
	username=$(echo "$lastUser"\@"$domain")
	# Update Computer Inventory Record
	jamf recon -endUsername $username
else
	echo "No Logins, exiting..."
fi

View solution in original post

6 REPLIES 6

cbrewer
Valued Contributor II
#!/bin/bash

# Get the last user
lastUser=$(defaults read /Library/Preferences/com.apple.loginwindow lastUserName)

echo "Running recon for ${lastUser}..."
jamf recon -endUsername "${lastUser}"

cbrewer
Valued Contributor II

If you want to manually add a domain suffix to lastUser:

lastUser="$(defaults read /Library/Preferences/com.apple.loginwindow lastUserName)@example.com"

robjschroeder
New Contributor III
#!/bin/bash

# Define domain suffix to add to username
domain="companyX.com"

# Get the last user of computer
lastUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`

if [ $lastUser != "" ]; then
	username=$(echo "$lastUser"\@"$domain")
	# Update Computer Inventory Record
	jamf recon -endUsername $username
else
	echo "No Logins, exiting..."
fi

@robjschroeder   This work great!!!   Thank you so much!

Awesome! I'm glad to see it worked for you!

NickGuru
New Contributor II

this would of worked if our company email standard was, example JDoe@company.com, but our email standard name is Joe.Doe@company.com . Good thing the policy only went out to about 30 users. I'll just click on each computer and manually enter their email address, then hit the search icon and it will find them and populate, their name, email, department, etc.