views:

645

answers:

3

Hi all, I've tried my best to find out a solution with the many script questions on Stack Overflow and the internet, but I can't seem to find the solution I need.

What I want to do is create a more automated and less clicking solution to remove all the Mobile cached user accounts on a system. I've been logging in and manually going to user accounts and removing the users one at a time by clicking the "-" button, then clicking "Delete Immediately" for the user data. This works, but is time consuming and I have better things to do with my time. So I knew there had to be a way to do this with a script.

I ran across this code:

for cuser in `dscl . -list /Users AuthenticationAuthority | grep LocalCachedUser | awk '{print $1}' | tr '/n' ' '`; do
    dscl . -delete /Users/$cuser
done

If I run this in terminal I get permission errors. So I figured I need to run it with sudo. So I started looking into creating AppleScripts to run the script, but I can't seem to find the right way to do it.

Any ideas? By the way, I'm new to scripting on the Mac, so please comment your code so I know whats happening, and so I don't just run some script code without know what it'll do. :)

Thanks

A: 

I don't have a mac handy so I can't verify if this would work.

Try running su -

Then running your script. If that works, try crontab -e

and adding an entry to run that script of yours.

Are you familiar with crontab? well if not google it if need be. But basically to run it every day at midnight you'd have something like 0 * * * * /path/to/script

See: http://en.wikipedia.org/wiki/Cron

Matt H
Could you construct your answer better? I'm not sure what "su -" is. Is that just sudo for short?
ScottN
su is short for "substitute user". It allows you to log in as root from a Terminal.
Quartz
+3  A: 

To perform a shell script with sudo or administrator privileges append with administrator privileges to the end of your do shell script line. For example:

do shell script "/path/to/script/file.sh" user name "adminusershortname" password "password" with administrator privileges

You can find more on Apple's technote dealing with do shell script

That said, saving this as a shell script and running the shell script using sudo would work just as well.

#! /bin/sh

for cuser in `/usr/bin/dscl . -list /Users AuthenticationAuthority | grep LocalCachedUser | awk '{print $1}' | tr '/n' ' '`; do
    /usr/bin/dscl . -delete /Users/$cuser
done

Save it as say removeUser.sh, use chmod to set it as executable (chmod 755) and then run it (sudo ./removeUser.sh)

Chealion
I tried running it in an apple script, but now I get a whole bunch of "delete: Invalid Path", which I assume is for each user it is attempting to delete. The AppleScript file is on the desktop. Does it need to be some other place? Do I need to give the delete command a more valid path?
ScottN
I removed the "| tr '/n' ' '" from the command as running the list command in Terminal was printing the users very weird. But now I have another problem. When I ran the AppleScript the first time and I didn't get an error, I assumed it worked. Checking the accounts list in system preferences still lists the users, so the delete didn't remove the user accounts completely from the system. But if I run the list command again, no users are printed in the Terminal window, so now the script has nothing to run. What am I doing wrong now? Is the "dscl . -delete" command do the same thing as GUI?
ScottN
Ok, They were still showing up in system preferences until I rebooted the system, then they were no longer there. I did have to manually remove the user folders. But that requires only a few clicks. I might be satisfied with this. It would be nice to remove the users folder at the same time.
ScottN
I added "" to the script and it removed the users folder at the same time. Works great, just how I wanted!
ScottN
Sorry, forgot to paste, I added "rm -rf /Users/$cuser" to the script, after dscl command.
ScottN
A: 

You can do this by editing your system's sudoers file. This will allow the account you use to run this script (via cron, etc.) the ability to run sudo without a password.

To edit the sudoers file you use visudo, but it must be run with admin permission. Try:

$ sudo visudo

Add a line like the following to the end of the file, replacing user_name with the user who will run your script. Note, use tabs between each field.

user_name    ALL=(ALL)     NOPASSWD:ALL

Now user_name should be able to type sudo and will not be prompted for a password.

Also note that visudo is a text editor that mirrors the vi editor and uses the same commands as vi.

jdl2003