GAM to print all Users in an OU that have Mail forwarding enabled?

2,459 views
Skip to first unread message

Steven Clayton

unread,
Sep 14, 2023, 3:33:35 PM9/14/23
to GAM for Google Workspace
I found some suggested commands but none seem to work on my end.    We are trying to determine which users have enabled mail forwarding and cant seem to be able to do it from Admin Console, looks like GAM should be able to, but I could some help with the proper command syntax.    Running GAM on a Mac if it makes any difference.
This message was sent from an official MKA email account.

Ian Crew

unread,
Sep 14, 2023, 3:43:48 PM9/14/23
to GAM for G Suite
Here’s the script that we use to get (a) all the settings and filter forwards, and (b) all of the users by OU. We then use BigQuery to merge the three together into a single report, but you could probably also do a lot of it with Looker Studio or even just Sheets.

#!/bin/bash


DATE=`date +%Y-%m-%d`

FORWARDSLOGFILE=“./logs/"$DATE"_email_forwards_update.txt"



/bin/killall ~/bin/gamadv-xtd3/gam  > $FORWARDSLOGFILE 2>&1

/bin/killall gam  >> $FORWARDSLOGFILE 2>&1


#get the forwarding


/home/app_gam/bin/gamadv-xtd3/gam config csv_output_row_filter "forward:regex:^.+$" csv_output_header_filter "User,forward,markread,archive,trash" auto_batch_min 1 num_threads 40 redirect csv - multiprocess todrive tdfileid 'FILE_ID_REDACTED' tdretaintitle tdsheet "Filter forwards" all users print filters >> $FORWARDSLOGFILE 2>&1



/home/app_gam/bin/gamadv-xtd3/gam config csv_output_row_filter "forwardTo:regex:^.+$" auto_batch_min 1 num_threads 40 redirect csv - multiprocess todrive tdfileid 'FILE_ID_REDACTED' tdretaintitle td

sheet "Settings forwards" all users print forwards >> $FORWARDSLOGFILE 2>&1



#copy the filter forwards to make a day-by-day set of sheets for historical purposes

/home/app_gam/bin/gamadv-xtd3/gam user ad...@example.edu copy drivefile 'FILE_ID_REDACTED' newfilename $DATE" Filter forwards"  parentid FOLDER_ID_REDACTED >> $FORWARDSLOGFILE 2>&

1


#copy the settings Forwards

/home/app_gam/bin/gamadv-xtd3/gam user ad...@example.edu copy drivefile 'FILE_ID_REDACTED' newfilename $DATE" Settings forwards"  parentid FOLDER_ID_REDACTED  >> $FORWARDSLOGFILE 2

>&1



#UPDATE THE LIST OF OUS VS. PEOPLE

/home/app_gam/bin/gamadv-xtd3/gam print users ou todrive tdfileid 'FILE_ID_REDACTED' tdretaintitle tdsheet "example.edu - Users by OU"  >> $FORWARDSLOGFILE 2>&1



cat "$FORWARDSLOGFILE" | /usr/bin/tr -d '[\200-\377]' | /usr/bin/tr '\015' '\n' | /bin/grep -B2 -A3 "Data uploaded to Drive File" | /bin/mail -s  "Email Forwards Reporting Results for $DATE" us...@example.edu,gro...@lists.example.edu


/bin/find /home/app_gam/email_forwards/logs -mtime +1 -empty -delete >> “./logs/"$DATE"_delete-empty-email-forwards-logs.txt" 2>&1


/bin/find /home/app_gam/email_forwards/logs -mtime +2 -exec /bin/gzip {} \; >> ./logs/"$DATE"_gzip-email-forwards-logs.txt" 2>&1



On Sep 14, 2023, at 12:29 PM, Steven Clayton <scla...@mka.org> wrote:

I found some suggested commands but none seem to work on my end.    We are trying to determine which users have enabled mail forwarding and cant seem to be able to do it from Admin Console, looks like GAM should be able to, but I could some help with the proper command syntax.    Running GAM on a Mac if it makes any difference.
This message was sent from an official MKA email account.

--
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/d24faa2d-482d-4280-8819-e5723567bdadn%40googlegroups.com.

Ross Scroggs

unread,
Sep 14, 2023, 3:54:35 PM9/14/23
to google-ap...@googlegroups.com
On Sep 14, 2023, at 12:29 PM, Steven Clayton <scla...@mka.org> wrote:

I found some suggested commands but none seem to work on my end.    We are trying to determine which users have enabled mail forwarding and cant seem to be able to do it from Admin Console, looks like GAM should be able to, but I could some help with the proper command syntax.    Running GAM on a Mac if it makes any difference.
This message was sent from an official MKA email account.

Mike Beane

unread,
Sep 14, 2023, 4:19:13 PM9/14/23
to google-ap...@googlegroups.com
I'm using gam-adv for reference.

From an OU perspective, we do this (reason why below) to review forwards:

gam ou "<name of OU>" show forwardingaddress, you should get a list of established forwarding addresses. I'll use "students" as my OU:

gam ou "students" show forwardingaddress

You can run gam user <target> show forward, to see if forwarding is enabled or not, but it's not the full picture.

gam user mort@unseenuniversity.edu show forward

User: mort@unseenuniversity.edu, Forward Enabled: False|True

image.png

Sure enough, my work account has Disable forwarding checked, that checks out.

However, even though my work account shows False, I know I have some forwards established to other internal addresses via filters (note that Ian's response looks at filter forwards as well).  Since filters require the target address to be defined, I use the following to list the targets (and if your policy was no external forwarding for an OU, you could programmatically clip anything that isn't internal or on an approved list for outbound forwards) out for review.

gam user mort@unseenuniversity.edu show forwardingaddress

# User:  mort@unseenuniversity.edu, Show 3 Forwarding Addresses (4/19)
#  Forwarding Address: rinc...@unseenuniversity.edu, Verification Status: accepted (1/3)
#  Forwarding Address: bur...@braseneckcollege.edu, Verification Status: accepted (2/3)
#  Forwarding Address: mus...@unseenuniversity.edu, Verification Status: accepted (3/3)

#2 is outside the domain and let's say that needed to be handled by policy, it could be handled via gam ala  gam user mo...@unseenuniversity.edu delete forwardingaddress bur...@braseneckcollege.edu.





On Thu, Sep 14, 2023 at 3:33 PM Steven Clayton <scla...@mka.org> wrote:
I found some suggested commands but none seem to work on my end.    We are trying to determine which users have enabled mail forwarding and cant seem to be able to do it from Admin Console, looks like GAM should be able to, but I could some help with the proper command syntax.    Running GAM on a Mac if it makes any difference.
This message was sent from an official MKA email account.

--

Ross Scroggs

unread,
Sep 14, 2023, 4:22:42 PM9/14/23
to google-ap...@googlegroups.com

Steven Clayton

unread,
Sep 15, 2023, 8:13:39 AM9/15/23
to GAM for Google Workspace
Thanks Everyone, Mikes suggestion of "gam ou "<name of OU>" show forwardingaddress, you should get a list of established forwarding addresses. I'll use "students" as my OU:"
Works for what I need.     Ideally it would only display the users with forwarding enabled vs showing all users and then you pick out the ones with forwarding but I can get the info I need with this.    

Is there anyway to use wildcards or have the command search nested OU's?    When running it against "Students" OU it didn't work because all students are in Grade Specific OU's within "Students"
For example...

gam ou "students" show forwardingaddress            (This gave me ZERO results)

gam ou "students/ClassOf2029" show forwardingaddress.   (This listed EVERY SINGLE Student in this OU


Thanks

Ross Scroggs

unread,
Sep 15, 2023, 10:41:25 AM9/15/23
to google-ap...@googlegroups.com
Steven,

To get an ou and sub ous do ous_and_children

gam ou_and_children "students" show forwardingaddress


Here's an Advanced Gam solution that should do what you want: see: https://github.com/taers232c/GAMADV-XTD3/wiki/Users-Gmail-Forwarding

# Get students with forwarding enabled
gam redirect csv ./StudentsWithForwarding.csv ou_and_children "/Students" print forward enabledonly

# Get forwarding addresses for students with forwarding enabled
gam redirect csv ./StudentsForwardingAddresses.csv multiprocess csv StudentsWithForwarding.csv gam user "~User" print forwardingaddresses

# Get forwarding address from students with filters that forward
gam config csv_output_row_filter "forward:regex:^.+$" redirect csv ./StudentsFilterForwards.csv ou_and_children "/Students" print filters

Ross
----
Ross Scroggs


Steven Clayton

unread,
Sep 15, 2023, 10:47:33 AM9/15/23
to google-ap...@googlegroups.com
That's GREAT!    Thanks so much.

New to GAM need to spend more time learning about all the possibilities.  



You received this message because you are subscribed to a topic in the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-manager/-5Q4lCYLdiE/unsubscribe.
To unsubscribe from this group and all its topics, 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/761ABF64-F10B-44C5-BCD6-9E8CAD14E9ED%40gmail.com.


--

Steven Clayton

ASSISTANT DIRECTOR OF TECHNOLOGY

FACULTY ADVISOR TO MKA.TV

MONTCLAIR KIMBERLEY ACADEMY

973.783.6605  |  mka.org

Follow MKA on facebook and twitter

Reply all
Reply to author
Forward
0 new messages