Best User-Detection Command for Scripts?

alexjdale
Valued Contributor III

Hi folks, with Python deprecated and throwing scary warnings to users when it's invoked by Jamf, we're looking to replace our trusty logged-in-user detection command that we use in scripts.  Self Service scripts work reliably with $3, but what method are you all using to detect the logged-in user for other scripts?  What works reliably every time, and doesn't pick up _mbsetupuser or similar accounts?

 

Thanks in advance for any suggestions!

1 ACCEPTED SOLUTION

sdagley
Esteemed Contributor II

@alexjdale Here's @arminBriegel 's pretty much definitive discussion on best practice for determining the logged in user: https://scriptingosx.com/2020/02/getting-the-current-user-in-macos-update/

 

View solution in original post

7 REPLIES 7

ljcacioppo
Contributor III

I've been using this without issue:

 

 

loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕 && ! /loginwindow/ { print $3 }' )

 

 

 

 

 

That's not supposed to be an Emoji...adding screenshot 

Screen Shot 2022-01-13 at 12.51.04 PM.png

Commands via Emojis would be "fun"!

I'm here for it. But I've started to dig into this and will hopefully get this resolved for you all shortly.

mm2270
Legendary Contributor III

That would be great to get this fixed @CalleyO The forum converting text to emojis really shouldn't happen inside a code block, for obvious reasons.

sdagley
Esteemed Contributor II

@alexjdale Here's @arminBriegel 's pretty much definitive discussion on best practice for determining the logged in user: https://scriptingosx.com/2020/02/getting-the-current-user-in-macos-update/

 

alexjdale
Valued Contributor III

Thanks, this was just what I was looking for!