Script request: Change full name to account name (System Pref/users & groups)

Johnny_Kim
Contributor II

Hey all,
Just a quick run down of my environment...we are running MBA OS X 10.7 in a active directory environment. We just found out the new centralized printing with has an issue with how OS X sends a print job. When the OS X sends a print job to our print server (smart look server), its sends the print as "Full Name" located in the System preference/users & groups. The smart look server looks at the username field and OS X is sending that information as "Full Name" instead of the account name(username).

When I change the "Full Name" to my "account name/username" (in my case "Kim, Johnny" to "jkim"), the printer sees the job is from "jkim" and is able to print.

I am thinking maybe a script I can push to pull the account name and rename the "Full Name" field. Also, for those who know AD, the username has to be pulled from "User logon name" located in the account tab in AD. Reason being is because we have users whose name is William Jones but logins as Bill Jones(bjones).

Ill take suggestions if there are any other more efficient ways.

Thanks in advance!!

2 ACCEPTED SOLUTIONS

Johnny_Kim
Contributor II

Finally got the script to work. I used the "system_profiler | grep "User Name:" | sed "s/^ User Name: ([^(]) (./1/g"" to pull the full name. It does take a few seconds to retrieve.

ShortName=`whoami`
FullName=`system_profiler  | grep "User Name:" | sed "s/^      User Name: ([^(]*) (.*/1/g"`
dscl . -change /Users/$ShortName RealName "$FullName" "$ShortName"

acdesigntech- I'm going to give your a try and see how it is. Thank you! EDIT: dscl . -read /Users/$shortname realname | awk 'print {$2}' returned a short name

View solution in original post

Johnny_Kim
Contributor II

Heres an update...

This did work, just not in casper. because of the time it took to retrieve the FullName, it timed-out and returned a fail.

acd- I must have been too tired when you first posted your script and decided to take another look again today.

You are right, it does work! the only change I had to do was add a "comma" between "$2$3" as it was returning the fullname as "Kim,Johnny" without the space.

So, after everyone's help and a lot of googling to understand bash...here is the script that I am hoping to work. I will test it out on Casper and update this again.

Karl - Ideally, I would prefer to force use of shortname instead of longname, but it did not work for me. I'll have to look into this in the near future.

Bentoms - It seems like they removed the com.apple.NetworkAuthorization in 10.7/10.8.

Again, thanks for everyones help!

ShortName=`whoami`
FullName=`dscl . -read /Users/$ShortName RealName | awk 'BEGIN {FS=": "} {print $1}'`
FullName2=`echo $FullName | awk '{print $2,$3}'`
dscl . -change /Users/$ShortName RealName "$FullName2" "$ShortName"

View solution in original post

29 REPLIES 29

Johnny_Kim
Contributor II

While I'm waiting, I am actually in the process of learning how to create applescripts and this is what I'm at. Just an FYI

tell application "System Events"
    set shortName to name of current user
    set fullName to full name of current user
end tell

tell application "System Preferences"
    activate
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "users & groups" of menu "View" of menu bar 1
        tell window "users & groups"
            set full name to shortName
        end tell
    end tell
end tell

mm2270
Legendary Contributor III

I'm not sure I'd do this via Applescript myself, unless your users are OK with "ghost in the machine" type of operations. It might freak some folks out to see System Prefs opening and operating on its own. Also consider that someone has to be logged in for this to happen. But I think that would be the case with a shell script also.

Take a look at dscl. Specifically the -change command will do what you want. Capture the variables of current full username and shortname in the script, then pipe it into something like this-

dscl . -change /Users/$ShortName RealName "$FullName" "$ShortName"

talkingmoose
Moderator
Moderator

I agree with Mike and he's given a good alternative.

My rule of thumb is to use AppleScript to script third party applications and use shell scripting for most everything else including Mac OS X preferences and settings. Unless you really want a user to see something like a Finder window opening then shell scripting is probably the better route for most things.

I suggest avoiding anything that uses System Events to click buttons or menus. That's kind of like remote controlling windows and should be a last resort.

If you don't know shell scripting then learning that may be a better alternative to AppleScript right now. It's very easy and even knowing how to write a simple shell script will take your administrative capabilities to new levels.

Johnny_Kim
Contributor II

Thanks for the help guys! I really do appreciate it.

I have been looking at shell scripting and I'll try what I can do (we are coming from a Windows environment and OS X is something new to me/us).

I will keep you guys posted.

Thanks again!

Johnny_Kim
Contributor II

Hey guys,
I didn't get far with the shell script. May I ask little more on the right path?

How do I capture the current full name so I can pipe it in a script?

I understand the "$" would be the variable field.

$shortname=jkim
$fullname=Kim, Johnny

And then using the "dscl . -change /Users/$ShortName RealName "$FullName" "$ShortName"" command will fill in the variables.

But in my case, i would need the names to pulled from the currently logged in user instead of being hard coded.

Any help is apprecaited!

mm2270
Legendary Contributor III

The first thing is, scripts run from the Casper Suite always run as the root user, not the person logged into the computer. Because of this, you can't just use the 'who' command to get the current user. If you search around here, you'll find about 5 or 6 different methods folks use to capture the current user name. Here's the one I use, just as an example-

/usr/bin/who | awk '/console/{ print $1 }'

That send "who" through "awk" which looks for "console" since the current logged in user owns the console, and then prints the first field. This returns the short name, or "jkim" in your example above. You can try that out in Terminal on your own system to confirm you get the results you expect.

You can also just use the built in "$3" variable from Casper, but that only works for login or logout scripts or from Self Service.

Once you store that in a variable, like so-

ShortName=`/usr/bin/who | awk '/console/{ print $1 }'`

You can then use dscl to grab the rest of your info. Try this-

FullName=`dscl . -read /Users/$ShortName RealName | tail -1`

That should return "Kim, Johnny" for you.

Test those both out and see how they work. In the dscl line above, if you don't get your full name returned, remove the "| tail -1" part and run it without it to see what it returns. In my case my full name gets placed on the line below RealName: which is why I piped it thru tail. There are other ways to get the next line.

If those work, that's all you need to get those 2 pieces into variables. Then use the dscl . -change command.

Johnny_Kim
Contributor II

mm,
First off, I really appreciate your help with this, thank you!

I've been playing with what you gave me for the past few hours.

I've debugged the script and found out using the command below will not work.```
FullName=`dscl . -read /Users/$ShortName RealName | tail -1'

It returns the below.

ShortName=whoami
whoami
+ whoami
ShortName=jkim
FullName=dscl . -read /Users/$ShortName RealName | tail -1
dscl . -read /Users/$ShortName RealName | tail -1
+ dscl . -read /Users/jkim RealName
tail -1
FullName=' Kim, Johnny'
dscl . -change /Users/$ShortName RealName "$FullName" "$ShortName"
+ dscl . -change /Users/jkim RealName ' Kim, Johnny' jkim
<main> attribute status: eDSAttributeNotFound
<dscl_cmd> DS Error: -14134 (eDSAttributeNotFound)
```

As you can see, it works fine until it trys to pull the "FullName" with quotations. And yes, I did use "whoami" for "ShortName".

So I tried "osascript -e 'long user name of (system info)'" but thats applescipt and couldn't figure out how to use that in bash.

Then tried "finger $ShortName | grep Name | awk -F "Name:" '{print $2}' but it only pulls partial name.

I can't find other places to pull the FullName from. I've dig through other scripts but no cigar!

acdesigntech
Contributor II

You can capture an AS return code in bash like so: myvar=$(osascript -e bladdy blah)

Also dscl . -read /Users/$shortname realname | awk 'print {$2}' should give you what you want...

Johnny_Kim
Contributor II

Finally got the script to work. I used the "system_profiler | grep "User Name:" | sed "s/^ User Name: ([^(]) (./1/g"" to pull the full name. It does take a few seconds to retrieve.

ShortName=`whoami`
FullName=`system_profiler  | grep "User Name:" | sed "s/^      User Name: ([^(]*) (.*/1/g"`
dscl . -change /Users/$ShortName RealName "$FullName" "$ShortName"

acdesigntech- I'm going to give your a try and see how it is. Thank you! EDIT: dscl . -read /Users/$shortname realname | awk 'print {$2}' returned a short name

acdesigntech
Contributor II

sorry, written while i was waiting impatiently for breakfast :P Here's a version that actually works

myVar=`dscl . -read /Users/$ShortName RealName | awk 'BEGIN {FS=": "} {print $1}'`
finalVar=`echo $myVar | awk '{print $2$3}'`
echo $finalVar

Johnny_Kim
Contributor II

acd, still didn't work.
This is what it returns.

ShortName=`whoami`
whoami
++ whoami
+ ShortName=jkim
FullName=`myVar=`dscl . -read /Users/$ShortName RealName | awk 'BEGIN {FS=": "} {print $1}'`
finalVar=`echo $myVar | awk '{print $2$3}'`
echo $finalVar`
myVar=
++ myVar=


echo $finalVar
finalVar=
++ echo
++ finalVar=
+ FullName=dscl
+ . -read /Users/jkim RealName
+ awk '{print $2$3}'
+ awk 'BEGIN {FS=": "} {print $1}echo'
/Users/jkim/Documents/username.sh: line 2: .: -r: invalid option
.: usage: . filename [arguments]
dscl . -change /Users/$ShortName RealName "$FullName" "$ShortName"
+ dscl . -change /Users/jkim RealName '' jkim
<main> attribute status: eDSAttributeNotFound
<dscl_cmd> DS Error: -14134 (eDSAttributeNotFound)

macguy
New Contributor II

Hi, I came across a similar problem with the print server taking to long to auth due to the Full name being supplied instead of the short name. The answer was to force the use of the short name by the following code:-

defaults write /Library/Preferences/com.apple.NetworkAuthorization UseShortName -bool YES

This seemed to fix the issue for me and printing.

Hope this helps

Karl

bentoms
Release Candidate Programs Tester

I guess these print queues are SMB?

I'd go with what Karl did, I did the same for some AFP stuff awhile back but I needed to change the key in: ~/Library/.GlobalPrefernces.plist

acdesigntech
Contributor II

Copy and paste exactly what i type. You have fullname being assigned to myvar being assigned, in the same command.

Assign shortname to myvar by using whoami, like you have been doing. But then copy and paste the rest of the code, it works, trust me

Johnny_Kim
Contributor II

macguy- That is exactly what I need, except its not working for me. I ran the command as sudo and it does ask for my password but still will not print as the shortname.

Any thoughts?

Johnny_Kim
Contributor II

Heres an update...

This did work, just not in casper. because of the time it took to retrieve the FullName, it timed-out and returned a fail.

acd- I must have been too tired when you first posted your script and decided to take another look again today.

You are right, it does work! the only change I had to do was add a "comma" between "$2$3" as it was returning the fullname as "Kim,Johnny" without the space.

So, after everyone's help and a lot of googling to understand bash...here is the script that I am hoping to work. I will test it out on Casper and update this again.

Karl - Ideally, I would prefer to force use of shortname instead of longname, but it did not work for me. I'll have to look into this in the near future.

Bentoms - It seems like they removed the com.apple.NetworkAuthorization in 10.7/10.8.

Again, thanks for everyones help!

ShortName=`whoami`
FullName=`dscl . -read /Users/$ShortName RealName | awk 'BEGIN {FS=": "} {print $1}'`
FullName2=`echo $FullName | awk '{print $2,$3}'`
dscl . -change /Users/$ShortName RealName "$FullName2" "$ShortName"

steventhemacman
New Contributor III

Hi, I am not able to get your script to work with 10.9. Is there some change that needs to be made? I'm not a scripter...; (

Johnny_Kim
Contributor II

@steventhemacman Here is the updated script. In my last post, "whoami" does not work with a login trigger as it will run as root.
I also have applied logic in this script. It will check if the shortname = fullname2, if same, ignore the script.

ShortName=$3
FullName=`dscl . -read /Users/$ShortName RealName | awk 'BEGIN {FS=": "} {print $1}'`
FullName2=`echo $FullName | awk '{print $2,$3}'`

if [ "$ShortName" == "$FullName2" ]; then
exit

else
    dscl . -change /Users/$ShortName RealName "$FullName2" "$ShortName"
fi

steventhemacman
New Contributor III

@Johnny.Kim that was fast. Unfortunately it didn't' work, spat this back at me...any ideas?

Thanks a ton!

Sending Wake On LAN command...
Opening SSH Connection to 10.64.120.42...
Authenticating...
Successfully authenticated.
Verifying Computer's Identity...
The MAC Address has been verified.
Checking Operating System Version...
Running Mac OS X 10.9.5 (13F34)
Verifying /usr/sbin/jamf...
/usr/sbin/jamf is current (9.51)
Verifying /Library/Preferences/com.jamfsoftware.jamf.plist...
Preparing Policy...
Executing Policy 2015-01-28 at 11:33 AM | sjonker | 1 Computer...
Mounting Main Distribution Point to /Volumes/CasperShare...
Running script resetfullname.sh...
Script exit code: 185
Script result: DS Error: -14009 (eDSUnknownNodeName)
change: Invalid Path
Submitting log to https://jss.bcpsk12.net:8443/
Finished.

Johnny_Kim
Contributor II

Who are you authenticating for the SSH connection as?

steventhemacman
New Contributor III

I'm just running it via casper remote while i'm testing, so the local hidden JSS account?

steventhemacman
New Contributor III

Just to clarify what I am trying to do, we want to make sure that everybody's full name, is the same at their user account name. We didn't have the system preference for Users & Groups restricted, so they could change their full name to whatever they want. Before this was never an issue, but some new software we are using is displaying the full name instead of the username.

Johnny_Kim
Contributor II

Can you try running this policy as a login trigger? I wonder if its trying to run it as the hidden JSS account.

steventhemacman
New Contributor III

OK, I will give that a try and see what happens....I might not have time this afternoon. I will update with a post.

mscottblake
Valued Contributor

$3 does not have any value in it unless you are running as a Self Service policy or as a login trigger.

If you are trying to get the currently logged in user, I suggest using something like what @mm2270][/url posted above ```
ShortName=$(/usr/bin/who | awk '/console/{ print $1 }')
```
This approach should be friendlier in more deployment methods.

steventhemacman
New Contributor III

@msblake

Thanks for the tip, but I'm not sure I have it correct?

#!/bin/sh

/usr/bin/who | awk '/console/{ print $1 }'

ShortName=/usr/bin/who | awk '/console/{ print $1 }'
FullName=dscl . -read /Users/$ShortName RealName | tail -1

else
dscl . -change /Users/$ShortName RealName "$FullName2" "$ShortName"
fi

steventhemacman
New Contributor III

@Johnny.Kim][/url][/url I did run it as a login trigger, and got this outcome: However, it did seem to work, I see the name is changed now...so we do have a solution, although it would be cool if I could do it to the current logged in user without a restart.

Executing Policy Reset full name...
Mounting Main Distribution Point to /Volumes/CasperShare...
Running script resetfullname.sh...
Script exit code: 54
Script result:
attribute status: eDSAttributeNotFound
DS Error: -14134 (eDSAttributeNotFound)

Johnny_Kim
Contributor II

Steven, you're piping in "fullname2" but the variable isn't there. Copy and paste the code below and give that a try in terminal. run with "sudo bash -x -v /path/to/file"

ShortName=`/usr/bin/who | awk '/console/{ print $1 }'`
FullName=`dscl . -read /Users/$ShortName RealName | awk 'BEGIN {FS=": "} {print $1}'`
FullName2=`echo $FullName | awk '{print $2,$3}'`

if [ "$ShortName" == "$FullName2" ]; then
exit

else
dscl . -change /Users/$ShortName RealName "$FullName2" "$ShortName"
fi

mm2270
Legendary Contributor III

I recommend switching the ShortName command to use:

ls -l /dev/console | awk '{print $3}'

The problem with the who | awk syntax is that if you happen to use Fast User Switching and have more than one account actually logged in, even if one is switched out, you'll get inaccurate results on the username. You could still run into problems even with the above command I recommend, but its only likely to happen in a case where another user is remoted into the Mac with ScreenSharing and "logged in" under another account, which probably doesn't happen much if at all.
But I agree that you should make your scripts as portable as possible and try not to rely on something like $3 exclusively. If you later decided to use that same script outside of a login or Self Service policy, you'd need to edit it to make it work. Starting off with something like:

ShortName=$(ls -l /dev/console | awk '{print $3}')

is going to serve you better long term, in my opinion.