Last active
March 26, 2025 07:40
-
-
Save 65/b5e9cee9b5812b487b8ae3e8256e262b to your computer and use it in GitHub Desktop.
Helper file for for Google Workspace (GSuite) Resellers using GAMADV-XTD3 and need to swap between resold customers regularly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# | |
# Author: Duncan Isaksen-Loxton | |
# Web: sixfive.io | |
# | |
# 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 ROOTPATH 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 folder called GAMOutput with a folder for each domain below | |
ROOTPATH="$HOME/GAMOutput" | |
# 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 10) | |
NORMAL=$(tput sgr0) | |
MAGENTA=$(tput setaf 5) | |
YELLOW=$(tput setaf 3) | |
CREATEOUTFOLDER=false | |
if [ ! -d "${ROOTPATH}" ]; | |
then | |
echo_error "ERROR: ${ROOTPATH} does not exist \n Please correct this before continuing by editing gamselect.sh and changing the path in ROOTPATH 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 | |
echo_green "SUCCESS: You can now use gam commands on $1" | |
CREATEOUTFOLDER=true | |
# look for the customer | |
STRING=$(gam info domain) | |
CUSTOMERID=$(echo $STRING | head -1 | cut -d' ' -f3) | |
echo_yellow "CUSTOMERID: ${CUSTOMERID}" | |
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) | |
if [[ "$STRING" == *"$VALIDCUSTOMER"* ]] && [ "$STRING" > 2 ]; then | |
echo_yellow "Customer Info: ${STRING}" | |
echo_green "This domain is validated to your Reseller account" | |
# 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) | |
echo_yellow "Found and creating config for ${1} \nCustomer ID: ${CUSTOMERID}" | |
echo -e "\n[$1]\ncustomer_id=${CUSTOMERID}\ndomain=$1" >> $FILE | |
gam select $1 save | |
echo_green "SUCCESS: You can now run gamselect ${1} when you need to switch customers" | |
CREATEOUTFOLDER=true | |
else | |
echo_error "Looks like ${1} is not one of your resold customers" | |
exit 1 | |
fi | |
fi | |
# echo_yellow "Output folder exists? ${CREATEOUTFOLDER}" | |
if [ "$CREATEOUTFOLDER" = true ]; | |
then | |
# Create folder in the output folder | |
cd "${ROOTPATH}" | |
GWOUTPATH="${1}/Google Workspace" | |
mkdir -p "${GWOUTPATH}" > /dev/null 2>&1 | |
cd "$ROOTPATH/${GWOUTPATH}" | |
if [ $? = 0 ] | |
then | |
echo_green "Output folder created: ${GWOUTPATH} can be used for output of CSV commands" | |
else | |
echo_green "Output folder ${GWOUTPATH} can be used for output of CSV commands" | |
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" | |
# Set the out put path into the env to make it easy to reference in other commands | |
export OUTPUTPATH=$(echo "${ROOTPATH}/${GWOUTPATH}") | |
fi | |
# Set the domain into the bash env to make it easy to reference in other commands | |
export DOMAIN=$(echo ${1}) | |
# Get all primary domains and put into a list | |
DOMAINLIST=$(gam print domains | awk -F, 'NR>1 {print $1}' | tr '\n' ',' | sed 's/,$//') | |
# Create a version of that list with quoted elements for easy use in python scripts | |
DOMAINLISTQUOTED=$(gam print domains | awk -F, 'NR>1 {print "'"'"'"$1"'"'"'"}' | tr '\n' ',' | sed 's/,$//') | |
# Add both to the terminal for later use | |
export DOMAINLIST=$(echo ${DOMAINLIST}) | |
export DOMAINLISTQUOTED=$(echo ${DOMAINLISTQUOTED}) | |
echo_green "\nOther domains on account: ${DOMAINLIST} / ${DOMAINLISTQUOTED}" | |
# set thte clientID for use in other commands | |
export CUSTOMERID=$(echo ${CUSTOMERID}) | |
# get the subscriptions, excluding cloudidentity | |
skus=$(gam config csv_output_header_filter skuId csv_output_row_filter "skuId:text!='1010010001'" redirect csv "./tmp.csv" print resoldsubscriptions customerid ${CUSTOMERID} 2>/dev/null) | |
if [ $? -eq 0 ]; then | |
# get the seat counts and a flag if there are any seats available | |
sku_status=$(gam csv ./tmp.csv gam info resoldsubscription ${CUSTOMERID} "~skuId" formatjson | jq -r '{ | |
skuName, | |
licensedNumberOfSeats: .seats.licensedNumberOfSeats, | |
seatStatus: (if .seats.numberOfSeats - .seats.licensedNumberOfSeats == 0 | |
then "No seats available :-(" | |
else (.seats.numberOfSeats - .seats.licensedNumberOfSeats | tostring) + " Seats available!" | |
end) | |
} | .skuName + ": " + .seatStatus') | |
echo_red "\n${sku_status}" | |
# tidy up | |
rm "./tmp.csv" | |
fi | |
else | |
echo "Positional parameter 1 is empty" | |
fi | |
} | |
# Set up an alias in this script to pass through to the gam command | |
function gam() { "$GAM" "$@" ; } | |
# Easy message outputs https://gist.github.com/JBlond/2fea43a3049b38287e5e9cefc87b2124 | |
echo_red() | |
{ | |
echo -e "\x1B[1;31m$1" | |
echo -e '\x1B[0m' | |
} | |
echo_green() | |
{ | |
echo -e "\x1B[1;32m$1" | |
echo -e '\x1B[0m' | |
} | |
echo_yellow() | |
{ | |
echo -e "\x1B[1;33m$1" | |
echo -e '\x1B[0m' | |
} | |
echo_warning() | |
{ | |
echo -e "\x1B[1;36m**\n** $1 \n**" | |
echo -e '\x1B[0m' | |
} | |
echo_error() | |
{ | |
echo -e "\x1B[1;31m**\n** $1 \n**" | |
echo -e '\x1B[0m' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment