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
Contributor
#!/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

13 REPLIES 13

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
Contributor
#!/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 III

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.

Lakshmana
New Contributor II

Hi

I need the help for the above script.The users are in Azure and need to update the last login user automatically on daily basis in Asset Inventory.I am new to Jamf Pro.Can anyone help me whether we need to put the above shell script to be run in all the user machine and then this will update it for the Asset inventory ? 

Hey @Lakshmana ! You would need to run this script with a policy once per day to populate the user and location username with the logged in user. You would also need to make sure you are collecting ldap information on your computers. That setting is in Computer Management > Inventory Collection

Lakshmana
New Contributor II

```

#!/bin/bash

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

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

if [ "$lastUser" != "" ]; then
# Extract the username without the domain suffix
username=$(echo "$lastUser" | cut -d "@" -f1)

# Add domain suffix to create the email address
email="$username"@"$domain"

# Update Computer Inventory Record
sudo jamf recon -endUsername "$username"

# Set the email address for the user
sudo jamf recon -email "$email"
else
echo "No Logins, exiting..."
fi
```

I am trying this method to get the last username information.But the username information is not always correct.Example)firstname.last@domain.com is the email id means i am getting this firstname.last as the username but for some users we have username@domain.com this becomes issue for me.
Example)User who logged in FirstnameLast is shown here because of this @domain.com is having error for me.

Issue 1)We have the email like this format for some users(firstname.letter@domain.com
Issue 2)We have other emails like in this format for some users(username@domain.com)


Do we have any other way to rewrite this script validate the username information with the Azure ?

I also in need to get the Full Name and Position of the user information into the Jamf if possible.

Do you have Azure AD connected via Cloud IdP in Jamf? You’ll also need the setting to collect LDAP data in your Computer Inventory Collection. But provided you have those two set up, it should pull in the users details into the computer inventory record after recon, provided there is a username in the record. 

Lakshmana
New Contributor II

@robjschroeder  Yes I have connected my Azure AD via Cloud IdP in Jamf.How to do the settings with the LDAP data ? 

 

Azure does not support LDAP here as far as I know.I have the users sync in the Google G-Suite which have LDAP here

Lakshmana
New Contributor II

I need one more help here.The users profile in the mac machine is not same as username of their email id.If we change it through a shell script that will create a new folder.how to make the changes in backend without making the data loss for the user folder in any case if its possible to do so ?