Shell Script for GAM automation / Removing users from all groups

2,678 views
Skip to first unread message

Joe Rodriguez

unread,
Jun 18, 2013, 10:12:52 PM6/18/13
to google-ap...@googlegroups.com
I put together this shell script, it automates many functions that were needed at my job.  I'm still working on it but all the major features work.  I'm running this with GAM 2.55.  Enjoy!

#! /bin/bash
gam="python /gam/gam.py" #set this to the location of your GAM binaries
clear
echo "         gApps Admin"
read -p "Enter email address to admin: " email
clear

while :
do
 clear
 echo "   M A I N - M E N U"
 echo "1. Set Vacation Message /Remove Forward"
 echo "2. Delete Signature"
 echo "3. Check Vacation Message"
 echo "4. Remove From All Groups"
 echo "5. Check Group Membership"
 echo "6. Perform All Tasks"
 echo "7. Admin Another User"
 echo "8. Exit"
 echo "Please enter option [1 - 8]"
    read opt
    case $opt in
     1) echo "************ Set Vacation Message / Remove Forward *************";
        read -p "Please enter vacation message: " vaca_message
        $gam user $email forward off
        $gam user $email vacation on subject 'Out of the office' message "$vaca_message";
        echo "Press [enter] key to continue. . .";
        read enterKey;;
     2) echo "************ Delete Signature ************";
        $gam user $email signature ' ';
        echo "Press [enter] key to continue. . .";
        read enterKey;;
     3) echo "************ Current Vacation Message ************";
        $gam  user $email show vacation;
        echo "Press [enter] key to continue. . .";
        read enterKey;;
     4) echo "************ Remove From All Groups ************";
        purge_groups=$($gam info user $email | grep -A 100 "Groups:" |cut -d '<' -f2 |cut -d '>' -f1 |grep -v 'Groups:')
           for i in $purge_groups
            do
               echo removing $i |$gam update group $i remove member $email
            done;
        echo "All groups removed press [enter] key to continue. . .";
        read enterKey;;
     5) echo "************ Check Group Membership ************";
        purge_groups=$($gam info user $email | grep -A 100 "Groups:" |cut -d '<' -f2 |cut -d '>' -f1)
        echo $purge_groups;
        echo "Groups have been checked [enter] key to continue. . .";
        read enterKey;; #joerod wuz here
     6) echo "************ Perform All Tasks ************";   
        read -p "Please enter vacation message: " vaca_message
        $gam user $email forward off
        $gam user $email vacation on subject 'Out of the office' message "$vaca_message";
        $gam user $email signature ' ';
        purge_groups=$($gam info user $email | grep -A 100 "Groups:" |cut -d '<' -f2 |cut -d '>' -f1 |grep -v 'Groups:')
           for i in $purge_groups
            do
               echo removing $i |$gam update group $i remove member $email
            done;
        echo "All tasks preformed press [enter] key to continue. . .";
        read enterKey;;
     7) echo "************ Admin Another User ************";
        echo working on it;       
        echo "Press [enter] key to continue. . .";
        read enterKey;;
     8) echo "Bye $USER";
        exit 1;; 
     *) echo "$opt is an invaild option. Please select option between 1-8 only"
       echo "Press [enter] key to continue. . .";
        read enterKey;;
esac
done




Joe Rodriguez

unread,
Jun 19, 2013, 5:06:37 PM6/19/13
to google-ap...@googlegroups.com
I've update this script, check out my git https://github.com/joerod/shell/blob/master/GAM_Automation.sh

Bieb

unread,
Jun 20, 2013, 3:04:41 PM6/20/13
to google-ap...@googlegroups.com
This script looks very interesting, as I was just removing 140 graduated students, from the various groups they were members of. But, I am a scripting newb.. Are you running this script on Windows? Mac? Linux? If on Windows, what does the script need for an extension? Do I need powershell? Any other things I need to make sure I have on a Windows box? (I can run from Linux if that is easier)
 
Thanks
Harold
 

On Tuesday, June 18, 2013 10:12:52 PM UTC-4, JoeRod wrote:

Joe Rodriguez

unread,
Jun 20, 2013, 3:48:31 PM6/20/13
to google-ap...@googlegroups.com
This is a shell script ie unix based, linux mac ect.  

Bieb

unread,
Jun 21, 2013, 11:27:43 AM6/21/13
to google-ap...@googlegroups.com
It seems to hang on #4 - Remove from ALL Groups. I can get a response from 5 to show the groups, but 4 never completes. I have left it run for almost 2 hours.
 
I had no problem suspending a user also, and checking a vacation message.
 
Thanks
Harold

Joe Rodriguez

unread,
Jun 21, 2013, 2:50:08 PM6/21/13
to google-ap...@googlegroups.com
Make sure the user is not suspended before you try and remove groups.  The script is working on our end, try running the script like this "bash -x gam.sh" this will give you debug when running the script.

Bieb

unread,
Jun 27, 2013, 10:41:33 AM6/27/13
to google-ap...@googlegroups.com
I ran the command:  sudo bash -x ./gam.sh  Here is where it hangs:
Selected #4 it gives the ***Remove from all Groups****
++ grep -A 100 Groups:
++ cut -d '<' -f2
++ grep -v Groups:
++ cut -d '>' -f1
 
Thanks
Harold

Joe Rodriguez

unread,
Jun 27, 2013, 5:49:48 PM6/27/13
to google-ap...@googlegroups.com
I've updated the script and fixed the problem, it seems to display an error but after the script is run Cpanel shows all groups removed.

Reply all
Reply to author
Forward
0 new messages