Skip to content

Instantly share code, notes, and snippets.

@65
Last active January 18, 2023 03:19
Embed
What would you like to do?
Helper file for for Google Workspace (GSuite) Resellers using GAMADV-XTD3 and need to swap between resold customers regularly
#!/bin/bash
#
#
# Author: Duncan Isaksen-Loxton
# Web: sixfive.com.au
#
# PREREQUISITES
# Must have GAMADV-XTDV3 installed https://github.com/taers232c/GAMADV-XTD3
# We group all our GAM related files in a folder called GAMConfig
# We leave GAM to install to its default location e.g. $HOME/bin/gamadv-xtd3/gam
#
# INSTALLATION
# Create a folder and add place this script in it e.g.
#
# mkdir ~/GAMConfig
# curl -s -S -L https://gist.githubusercontent.com/65/b5e9cee9b5812b487b8ae3e8256e262b/raw/4d4c1faef49d2d0e3036452b44e3f5a5156bdb93/gamselect.sh > ~/GAMConfig/gamselect.sh && chmod +x ./GAMConfig/gamselect.sh
# Customise the CSVOUTDESTINATION as required
# When used this will create a folder below this with each domain to sort generated CSV into folders
# Edit your ~/.profile or or ~/.bashrc or ~/.zshrc and add the line:
# source ~/GAMConfig/gamselect.sh
#
# echo 'source ~/GAMConfig/gamselect.sh' >> ~/.zshrc
#
# Restart your terminal
# GAMADV-XTDV3 Config
# When you start your gam config you will need to point it to the GAMConfig directory
# gam config config_dir ~/GAMConfig verify
# USAGE
# from your command line
# gamselect domainame.com
#
function gamselect(){
# Set the path to the GAM you want to use
GAM=$HOME/bin/gamadv-xtd3/gam
# Path to the folder for your GAM CSV output
# We use a shared drive called GAMoutput with a folder for each domain below
CSVOUTDESTINATION="$HOME/"
# Mostly won't change
FILE=$HOME/GAMConfig/gam.cfg
# May need to change when Google change API output
VALIDCUSTOMER='Customer Domain Verified: True'
# Colors
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
MAGENTA=$(tput setaf 5)
YELLOW=$(tput setaf 3)
CREATEOUTFOLDER=false
if [ ! -d "${CSVOUTDESTINATION}" ];
then
printf "$RED\nERROR$MAGENTA ${CSVOUTDESTINATION}$NORMAL does not exist \n Please correct this before continuing by editing gamselect.sh and changing the path in CSVOUTDESTINATION at the top of the file\n"
return 0
fi
if [ "$1" != "" ]; then
if [ "$1" = "reseller" ] || [ "$1" = "default" ]; then
#
# use the default profile
#
$GAM select default save
elif grep -q "$1" "$FILE"; then
#
# Already in the config, so straight switch to it
#
$GAM select $1 save
printf "$GREEN\nSUCCESS$NORMAL You can now use gam commands on $MAGENTA$1$NORMAL \n"
CREATEOUTFOLDER=true
else
#
# Not yet configured, so lets make it happen
#
# switch to reseller first
$GAM select default save
# look for the customer
STRING=$($GAM info resoldcustomer $1)
printf "\nCustomer Info:\n${STRING}\n" >&2
if [[ "$STRING" == *"$VALIDCUSTOMER"* ]] && [ "$STRING" > 2 ]; then
printf "$GREEN\nThis domain is validated to your Reseller account\n$NORMAL"
# Find the customerid from first line of output, split the string on spaces, and get the third item format is 'Customer ID: xxxxxx'
CUSTOMERID=$(echo $STRING | head -1 | cut -d' ' -f3)
printf "\nFound and creating config for$RED ${1} $NORMAL\nCustomer ID:$RED ${CUSTOMERID}\n\n$NORMAL"
echo -e "\n[$1]\ncustomer_id=${CUSTOMERID}\ndomain=$1" >> $FILE
$GAM select $1 save
printf "$GREEN\nSUCCESS$NORMAL You can now run$MAGENTA gamselect $1$NORMAL when you need to switch customers \n"
CREATEOUTFOLDER=true
else
printf "$RED\nERROR$NORMAL Looks like $MAGENTA${1}$NORMAL is not one of your resold customers\n\n"
fi
fi
echo $CREATEOUTFOLDER
if [ "$CREATEOUTFOLDER" = true ];
then
# Create folder in the output folder
cd "${CSVOUTDESTINATION}"
mkdir ${1} > /dev/null 2>&1
if [ $? = 0 ]
then
printf "\nOutput folder created: $GREEN ${CSVOUTDESTINATION}${1} $NORMAL can be used for output of CSV commands\n"
else
printf "\nOutput folder$GREEN ${CSVOUTDESTINATION}${1} $NORMAL can be used for output of CSV commands\n"
fi
printf "\nThe following environment variables are available in this session: \n $YELLOW \$OUTPUTPATH$NORMAL - provides the path to the domain folder \n $YELLOW \$DOMAIN$NORMAL - the domain in use \n"
printf "\ne.g.\n Find all drive files shared with 'anyoneCanFind'\n gam config auto_batch_min 1 redirect csv $YELLOW\$OUTPUTPATH$NORMAL/anyonecanfind.csv multiprocess all users show filelist query \"visibility = 'anyoneCanFind'\" $NORMAL\n"
cd "${1}"
# Set the out put path into the env to make it easy to reference in other commands
export OUTPUTPATH=$(echo ${CSVOUTDESTINATION}${1})
fi
# Set the domain into the bash env to make it easy to reference in other commands
export DOMAIN=$(echo ${1})
else
echo "Positional parameter 1 is empty"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment