Run script as sudo with graphical password entry

New Contributor II

Is there a way to run the sudo command in a Shell script but have it ask for the password in a GUI window similar to the one attached?



Contributor III


1) What are you trying to accomplish?

2) Do you need this username and password to actually be authenticated/verified?

New Contributor II

Created a script that deletes all of the files for office 2011. see here Some of the files require the command to be run with the sudo command. To make it more user friendly, I would like for it to prompt for the password vs being typed in the command prompt

Legendary Contributor III

Why not run this through a Self Service policy or some other way via Casper? It would run it as root to start with, so no user level authentication required.

New Contributor II

We do not currently have casper in place fully yet. Once it is, then that would be the ideal way.

Legendary Contributor III

Oh, well, that's a whole different ballgame then.

You may have luck using an Applescript call to get the dialog to come up.

/usr/bin/osascript << EOF
set fullPath to do shell script "echo "scripts full path goes here"
do shell script "sudo " & fullPath with administrator privileges

I believe that will send up the authentication dialog. Only issue is that it says AppleScript Editor wants to make changes, or something like that.

Contributor III

Well, one of the methods we've used this for was they keychain first aid self service that used CocoaDialog to pop up this very window so they could enter their current username and password to then have the script in the background generate a new keychain.

Run through this thread for some ideas. I specifically used Andrina's stuff that's referenced. I'm trying to find the original post of that. She was at the JNUC one year and presented all sorts of helpful items...especially once you have Self Service available to users. Her script is linked in that thread though.

my link text

New Contributor II

Here is the script I have so far. currently to run it I do "sudo sh" I would like for the sudo command to be included in the script and have it prom for a password in a guy window. I tried the applescript way but because the script has so many lines and paths I was having issues with syntax.


osascript -e 'tell app "System Events" to display dialog "This will remove Microsoft Office 2011 and all of its components. Any files or documents created by these apps will not be touched. Click Ok to continue."'
echo After uninstall you will need to reinstall Silverlight if it was previously installed

Remove the Microsoft Office 2011 folder

rm -rfv /Applications/Microsoft Office 2011

Remove Office preferences

rm -rfv ~/Library/Preferences/*
rm -rfv /Library/PrivilegedHelperTools/
rm -rfv /Library/LaunchDaemons/

Remove the license file

rm -rfv /Library/Preferences/
rm -rfv ~/Library/Preferences/ByHost/*

Delete Application support

rm -rfv /Library/Application Support/Microsoft/

Remove receipts

rm -rfv /private/var/db/receipts/*

Delete Application support in User Folder

rm -rfv ~/Library/Application Support/Microsoft/Office

Rename Microsoft User Data

cd ~/Documents
rm -mv Microsoft User Data ~Microsoft User Data osascript -e 'display alert "Complete" message "Microsoft Office is now uninstalled"'

Valued Contributor

make the script a postinstall and put it in a package