Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript to create Admin Account

This is what I have but it keep failing.

Any help would be great



set T_password to text returned of (display dialog "Set Password" default answer "")

set longUser_name to text returned of (display dialog "Set Long Username" default answer "")

set user_name to text returned of (display dialog "Set Username" default answer "")

set userID to 600

set groupID to 1001


if (do shell script "/usr/bin/dscl . -search /Users name " & user_name) is not "" then

display alert "User name " & user_name & " already exists, modify the user name and try it again "

return

end if


-- create account, if (the user ID or the group ID) already exists, loop until the id is unique

do shell script "declare dscl=/usr/bin/dscl;t_uid=" & userID & ";g_uid=" & groupID & ";userA=" & user_name & "

while [[ -n \"$($dscl . -search /Users uid $t_uid)\" ]]; do

t_uid=$[t_uid+1]

done

while [[ -n \"$($dscl . -search /Users uid $g_uid)\" ]]; do

g_uid=$[g_uid+1]

done

$dscl . -create /Users/\"$userA\"

$dscl . -create /Users/\"$userA\" UserShell /bin/bash

$dscl . -create /Users/\"$userA\" RealName " & longUser_name & "

$dscl . -create /Users/\"$userA\" UniqueID $t_uid

$dscl . -create /Users/\"$userA\" PrimaryGroupID $g_uid

$dscl . -create /Users/\"$userA\" NFSHomeDirectory /Users/\"$userA\"

$dscl . -passwd /Users/\"$userA\" " & T_password & "

$dscl . -append /Groups/admin GroupMembership \"$userA\"

/usr/sbin/createhomedir -l -u \"$userA\"

/bin/echo \"Admin account: \"$userA\" successfully created\"" with administrator privileges

display alert the result


I get this error


User uploaded file

iMac, OS X Server

Posted on Mar 28, 2014 2:58 PM

Reply
9 replies

Mar 28, 2014 3:36 PM in response to appleexpert1

I suggest you assign the long string to a variable, then you can print it out & see what you are passing.


btw: to make someone adming, you add the user to the group: admin


(*


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



Author: rccharles

For testing, run in the Script Editor.

1) Click on the Event Log tab to see the output from the log statement

2) Click on Run


*)



on run

-- Write a message into the event log.

log " --- Starting on " & ((current date) as string) & " --- "

-- debug lines

set desktopPath to (path to desktop) as string

log "desktopPath = " & desktopPath

end run

Mar 31, 2014 10:34 AM in response to Mark Jalbert

Ok I reworked the script but it fails.


set userPass to text returned of (display dialog "Set Password" default answer "")

set longUser to text returned of (display dialog "Set Long Username" default answer "")

set shortUser to text returned of (display dialog "Set Username" default answer "")

set userID to 502

set groupID to 80


set theCommand to ""


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 scripttheCommand with administrator privileges


Error:



tell current application

do shell script "/usr/bin/dscl / -create /Users/kyle;/usr/bin/dscl / -create /Users/kyle UserShell /bin/bash;/usr/bin/dscl / -create /Users/kyle RealName 'kyle';/usr/bin/dscl / -create /Users/kyle UniqueID 502;/usr/bin/dscl / -create /Users/kyle PrimaryGroup 80;/usr/bin/dscl / -create /Users/kyle NFSHomeDirectory /Users/kyle;/usr/bin/dscl / -passwd /Users/kyle k;/usr/bin/dscl / -append /Groups/admin GroupMembership kyle" with administrator privileges


--> error "Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid." number 69

Result:

error "Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid.

Data source (/) is not valid." number 69

Mar 31, 2014 11:32 AM in response to appleexpert1

Before going off and trying to do this with a script have you tried the command from the command line to make sure you have a working command. Debuging BOTH the script and the underlying command in the script just makes you're like harder.


Get dscl working from the command line and then putting that into the script will be a lot easier.


As for what goes there have a look at the man page for dscl, it will depend on what you are doing and your setup.


BTW copying code from someplace and running on your system without understanding it is a sure-fire way to do something



regards


Message was edited by: Frank Caggiano - Also you look to be hardcoding UID into the script. Won't that be a problem when multiple users get the same UID?

AppleScript to create Admin Account

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.