Apple script needed for creating a local admin account

Hi Everyone,

I need a apple script which will create a local user account with admin privileges. I have tried doing this using following unix commands:

[b]dscl . -create /Users/test1

dscl . -create /Users/test1 UserShell /bin/bash

dscl . -create /Users/test1 RealName “Test1”

dscl . -create /Users/test1 PrimaryGroupID 1000

dscl . -passwd /Users/test1 123456

dscl . -append /Groups/admin GroupMembership test1[/b]

This scripts will create a user account but I can’t login to the machine using that account for some reason.

Thanks,

Try something like this

-- Set variables
set shortUser to "ghouse"
set longUser to "Dr. Gregory House"
set userPass to "infectious"
set userID to "600"
set groupID to "1001"

set theCommand to ""

-- Build the dscl command
set theCommand to theCommand & "/usr/bin/dscl / -create /Users/" & shortUser & ";"
set theCommand to theCommand & "/usr/bin/dscl / -create /Users/" & shortUser & " UserShell /bin/bash;"
set theCommand to theCommand & "/usr/bin/dscl / -create /Users/" & shortUser & " RealName " & quoted form of longUser & ";"
set theCommand to theCommand & "/usr/bin/dscl / -create /Users/" & shortUser & " UniqueID " & userID & ";"
set theCommand to theCommand & "/usr/bin/dscl / -create /Users/" & shortUser & " PrimaryGroup " & groupID & ";"
set theCommand to theCommand & "/usr/bin/dscl / -create /Users/" & shortUser & " NFSHomeDirectory /Users/" & shortUser & ";"
set theCommand to theCommand & "/usr/bin/dscl / -passwd /Users/" & shortUser & space & userPass & ";"
set theCommand to theCommand & "/usr/bin/dscl / -append /Groups/admin GroupMembership " & shortUser

do shell script theCommand with administrator privileges

I have test this script and works great except I have to change slashes (/) with periods (.), in order to run on Leopard machine.

Do you know if I want to run this on remote machine using apple remote desktop then what do I need to do? I tried copying and pasting thru “send unix command” task but that did not work.

Thanks,

Well if you just want to run the code as a unix command then strip out all the AppleScript bits until you’re left with something like thi
/usr/bin/dscl / -create /Users/ghouse;/usr/bin/dscl / -create /Users/ghouse UserShell /bin/bash;/usr/bin/dscl / -create /Users/ghouse RealName ‘Dr. Gregory House’;/usr/bin/dscl / -create /Users/ghouse UniqueID 600;/usr/bin/dscl / -create /Users/ghouse PrimaryGroup 1001;/usr/bin/dscl / -create /Users/ghouse NFSHomeDirectory /Users/ghouse;/usr/bin/dscl / -passwd /Users/ghouse infectious;/usr/bin/dscl / -append /Groups/admin GroupMembership ghouse

and then when you run it run it as the “root” user.

Yes, this script creates an account and I can login with that account but once I log into a machine using new account then it give me error that user’s home folder is not at usual place. Currently we have other account’s home folders at this location, “/Users/”. Does anyone know how to fix that problem?

Thanks,

Is this on Leopard or Tiger? I have tested the script in the form I posted “/” rather than “.” on Tiger and I had no problems. I can test on Leopard tomorrow though.

I have tried to run on Leopard and that is why I have to change slash (/) to period (.). But it still gives me home folder error.

Thanks,

I’m not in the office yet so I am unable to test this, but try changing this line

set theCommand to theCommand & "/usr/bin/dscl . -create /Users/" & shortUser & " NFSHomeDirectory /Users/" & shortUser & ";"

to this line

set theCommand to theCommand & "/usr/bin/dscl . -create /Users/" & shortUser & " home /Users/" & shortUser & ";"

It still comes up with same error again and I can’t find new users profile folder any where on this disk.

Can anyone tell me why am I getting the home folder error?

The user’s home folder is not at usual place and I am unable to finder user profile folder any where on the system disk.

Thanks,

interesting and good to know, I was under the impression that it was not required (which is indeed the case under Tiger).

Thanks James, this script works great. Since this was an apple script which I saved and copied over using ARD to the target machine then I ran it locally and it created an account and a home drive folder at 2 different locations (/Users/ and under the domain folder which is fine with me). Another thing that I experienced that after creating an account once I tried to login to target machine using newly created account then it showed me 2 different names (one was display name and other once was username) and I have to select in order to continue. This is what I have changed:

Do you know why am i getting those two different names?

How can I use this script as a unix command?

Thanks,

So this is the Unix script that I sent to the target machine using ARD:

This script creates a user properly but shows that it created a home directory as well. Once I logged into target machine using new account then I receive the same error (home folder for user is not located in the usual place or can not be accessed).

Can anyone tell what is wrong with this script?

Thanks,

Well a quick observation is that you left out the -l from the example that was posted above. According to the man page

-l creates home directories for users defined in the local directory domain.

Since we are creating a local account and not one defined by a Directory Service I would try adding that option back in and seeing what happens.

So I have added the “-l” syntax and executed that command again using root account from ARD but still no home folder and I have received the same error message.

One thing that I don’t understand is this that what is the difference between the apple script (that you posted earlier) and this unix script. With apple script account and home folder was created successfully but only issue with that one that a person has to login to all the machines and manually execute apple script. Is there a way that I can remotely run that script on all the machines using ARD?

Thanks,

Hi Jacques,

That script worked the way I wanted but I am not clear on this:

  1. Do I have use sudo sh -c ’ this if I am running this script from the ARD as a root user?

Thanks,

Thank you.

This worked for me using ARD in SL 10.6.2, but I received an error when sending it a user with Leopard 10.5.6. It Did create the admin account but didn’t create a home folder so you cannot access any settings requiring the default password. Any thoughts? Thanks!