Running GAM command from Shell Script

1,532 views
Skip to first unread message

Ahmad Rzayev

unread,
Jul 24, 2022, 7:35:44 PM7/24/22
to GAM for Google Workspace
Hello all,

Trying to run a simple script using gam, but getting error: 
format.sh: line 9: gam: command not found

The Script is as follows:
#!/bin/sh -e
EMAIL="$1"
NAME="$2"
LAST="$3"
PASS="$4"
ORG="$5"
DEP="$6"
sed -i .bak -e 's|<New Email>|'"${EMAIL}"'|g' -e 's|<First Name>|'"${NAME}"'|g' -e 's|<Last Name>|'"${LAST}"'|g' -e 's|<Password>|'"${PASS}"'|g' -e 's|<Org Name>|'"${ORG}"'|g'  -e 's|<org dept>|'"${DEP}"'|g' /Users/username/Documents/Gam/onboarding.txt
gam batch /Users/username/Documents/Gam/onboarding.txt

Gam is installed in default path, e.g. /Users/username/bin/gam
Tried: adding gam to $PATH :  export PATH="~/bin/gam/:$PATH" 
Also,as I know that aliases do not work well in shell script, tried adding this line to the script body: gam="/Users/username/bin/gam/gam"
Any suggestions?  Thank you in advance.

Ahmad

Maj Marshall Giguere

unread,
Jul 24, 2022, 9:13:34 PM7/24/22
to google-ap...@googlegroups.com
Ahmad;

Are you using GAM or GAMADV?

Your sed looks a bit funky to me and unnecessary. Also why not use bash?  Here's a bash script I use to do one-off account creation. Please note it depends on GAMADV-XTD.  You will need to fill out the ORGUNIT array with your "ou" paths, also I use a user defined schema (Member) to track member info, you can change that to Google's user info attributes if you like.  I ditched Google's info schema in favor of a user defined schema.  You will notice that I use a template welcome message, you can remove that if you like. As a suggestion you can put an "echo" command in front of the gam command line just to see what the output command will look like as a debugging aid.  Feel free to modify the script to suit your requirements.

#!/bin/bash -e
# Quick & Dirty build a Google Workspace account
# Outputs an appropriate gam command to create an account

# History:
# 01Mar19 MEG Created.
# INPUT:
# $1 - base account name
# $2 - Member ID
# $3 - first name
# $4 - last name
# $5 - Unit number
# $6 - Member Type
# $7 - Password
# $8 - Notification email address
# OUTPUT: appropriate gam command to create an account

# Path to email welcome message template
EMAIL_TEMPLATE=$HOME/work/Templates/new-user-email-template.html

# Domain in which to create account
DOMAIN="example.com"

# Help message
USAGE="Usage: $(basename $0) args\n
Build a gam command to create a Google account\n
---You must have gamadv-xtd installed and have a Google application key---\n
WARNING: $(basename $0) does NOT check for duplicate account names.\n
\tARGS:\n
\t\$1 - new account base name\n
\t\$2 - Member ID\n
\t\$3 - First name\n
\t\$4 - Last name \"last name\"\n
\t\$5 - Unit number: 000-999\n
\t\$6 - Member Type:\n
\t\$7 - Password\n
\t\$8 - Notification email address\n"

declare -A ORGUNIT
ORGUNIT[000]="/000"
ORGUNIT[001]="/001"
ORGUNIT[001]="/002"
ORGUNIT[001]="/003"

if (( $# < 8 )); then
    echo -e $USAGE
    exit 1
fi

#echo "WARNING: $(basename $0) does NOT check for account name collisions!"
#echo "Be absolutely sure the account name is UNIQUE to $DOMAIN before executing"
/usr/local/bin/gam create user $1@$DOMAIN givenname "\"$3"\" familyname \""$4"\" orgunitpath \""${ORGUNIT[$5]}"\" Member.ID $2 Member.Type $6 Member.Unit $5 password $7 changepassword true notify "$8" subject "\"Welcome to your NH Wing account\"" file $EMAIL_TEMPLATE html true



V/r

Maj Marshall E Giguere

NH Wing Director of IT

Civil Air Patrol, U.S. Air Force Auxiliary

GoCivilAirPatrol.com

nhwg.cap.gov

Volunteers serving America's communities, saving lives, and shaping futures.



--
You received this message because you are subscribed to the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/996ce37c-1e2a-4089-89e8-ac426becea63n%40googlegroups.com.

Kim Nilsson

unread,
Jul 25, 2022, 4:41:48 AM7/25/22
to GAM for Google Workspace
Nicely done, Marsh!

If possible, could you also share your email template?
I noticed you aren't using any tag replace commands here, so perhaps you are only using the default tags, if at all?

/Kim

Maj Marshall Giguere

unread,
Jul 25, 2022, 10:09:29 AM7/25/22
to google-ap...@googlegroups.com
Kim;

I could share the template but it's specific to our organization, so here's a stripped down version as a sample (see below).  The magic in the template is using the GAM built in keywords #user#, #password#, #givenname#, #familyname#.  GAM will automatically fill in the template fields with the values from the account create command.  

HTML email template for account creation notification:
you can use plain text instead, just don't use the "html" keyword for the template, use "text" instead.
=====================================================================================
<div dir=3D"ltr">Dear #givenname#:
</div>
<div>
<p>
Welcome #givenname# #familyname# to your new email account. An
account has been created for you because you are listed as a member.
</p>
<p>
Your new email account is: #user#<br/>
Your temporary password is: #password#<br/>
You will be prompted to change your password when you login to your account.
</p>
<p>
This account will allow you to receive messages and notifications
from us, as well as access the user portal at <a href="https://example.com">example.com</a>
You may start using your account by signing in at:
https://www.google.com/accounts/AccountChooser?Email=#user#&continue=https://apps.google.com/user/hub
</p>
<p>
If you prefer to use OutLook, Thunderbird or a different email client please use the following link for instructions on setting up your client.
</p>
<p>
https://support.google.com/mail/answer/7126229?authuser=1&visit_id=1-636469171631663641-2012628328&hl=en&rd=1
</p>
<p>
<p>
  Use your NH email account in a professional manner and in line
  with our core values.
</p>
<p>
Welcome aboard.
</p>
</div>
--
<div id="signature_block">

</div>

============================================================================================

Maj Marshall E Giguere

NH Wing Director of IT

Civil Air Patrol, U.S. Air Force Auxiliary

GoCivilAirPatrol.com

nhwg.cap.gov

Volunteers serving America's communities, saving lives, and shaping futures.


--
You received this message because you are subscribed to the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.

Maj Marshall Giguere

unread,
Jul 25, 2022, 11:57:46 AM7/25/22
to google-ap...@googlegroups.com
Kim;

It just hit me that it is possible to check for existing accounts simply.  Include the following command before the create.  Hmmm... I could include a switch to set the DOMAIN.

# check for existing account
gam info user $1@$DOMAIN >/dev/null &>/dev/null && echo "ERROR:Duplicate account."; exit 1


Maj Marshall E Giguere

NH Wing Director of IT

Civil Air Patrol, U.S. Air Force Auxiliary

GoCivilAirPatrol.com

nhwg.cap.gov

Volunteers serving America's communities, saving lives, and shaping futures.


On Mon, Jul 25, 2022 at 2:41 AM Kim Nilsson <there.is.no...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages