Primary User

MarcosMunoz
New Contributor III

Is there a better way to detect a machine's primary user? I have an Extension Attribute that will supposedly gather this information. For the most part it does an OK job. But, I always have to go back and clean up after the script because it always lists a random user as being the owner of 15 to 20 machines. I can honestly say I have no idea how this duplication is happening.

Is anyone else having better luck? Are you able to gather Primary User information correctly and accurately?

This is the script I am using to gather the Primary User:

#!/bin/sh

TopUser=$( last -t console | awk '{print $1}' | sort | uniq -c | sort -n | awk 'END{print $NF}' )

echo "<result>$TopUser</result>"

I have also started using a different EA that will display the "Last Logged-on User" to try and match the two. But, that's not really being much help.

Here's the "Last Logged-on User" script:

#!/bin/sh

lastUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`

if [ $lastUser == "" ]; then
    echo "<result>No logins</result>"
else
    echo "<result>$lastUser</result>"
fi

Any help would be greatly appreciated.

Thanks!

1 REPLY 1

davidacland
Honored Contributor II

The main issue is the frequency where extension attributes are being updated. They work well for fairly static values, but for things like this they are either updated too infrequently or at the wrong time.

For this type of information we create a static EA field, then run a script, triggered by a policy to update the EA via the API. that gives you the ability to target just that EA for updating, and gives you control over when it runs.

The other thought is regarding your definition of primary user. I've heard people describe it differently so I wondered what you are looking to populate that field with? That may change the EA script syntax or general approach.