Current logged in user not working with Extension Attribute?

jrogersnh
New Contributor III

I'm running into an issue with a Python script where if I run it locally (on any machine) it outputs the correct result. Essentially the script is just looking for the existence of a couple .plist files as of now.

fc6338b6e85040e093ea8fedae84d1cc
However, when I upload the script into an extension attribute, something goes wrong with getting the current logged in user and messes with the filepath I'm pulling in the script. Any ideas what might be causing this? I'm assuming it must be pulling 'root' as the user when run by jamf or something? Is there a way around this?

3 REPLIES 3

ryan_ball
Valued Contributor

@jrogersnh Post the entire script here using the >_ button. I could easily rewrite this in bash for you to test.

chriscollins
Valued Contributor

@jrogersnh this is because of how you are gathering the current user. When EAs run, they are run as root. Using id in the way you are isn’t getting the active console user, it’s getting the username of the person running the command itself. You should use one of the many ways that are listed on here that gather the active console user. You should also make sure you account for when there is no active user logged into the machine as well.

chriscollins
Valued Contributor

Since you’re using python, you could use this:

from SystemConfiguration import SCDynamicStoreCopyConsoleUser

username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]
username = [username,""][username in [u"loginwindow", None, u""]]