Hijacking Google Calendars - A Script
I didn't think it was possible, but Google Workspace's options for controlling permissions regarding Calendars is actually worse than those of Google Drive. Really the only way to get a list of all the calendars for your domain, is to use GAM. Setting up GAM has been covered ad-nauseum elsewhere, but what I'd like to share here is a Bash script that will take run through every user in your domain, find every calendar they are a member of, and dump that into a CSV file.
Next it will scrub through that file, ignore any personal Calendars, Google Classroom calendars, and calendars not owned by your organization.
Finally it will add the specified user as an owner (Calendars can have multiple owners!) and add the Calendar as visible for the specified user.
Here's a simplification of the more important commands, along with a description of each. The most current version of the script, including safety checks, is available on my GitLab.
#!/bin/bash
# Set GAM Location
gam="/home/administrator/bin/gam7/gam"
# The Google OU in which to search for users
OU="Staff"
# The Calendar admin account
calmaster="Calendar.Admin"
# Create a temp folder named the same as the script, without the extension
dir=$(basename "$0" .sh)
mkdir $dir
cd $dir
#### Add headers to users file
echo "primaryEmail" > ./users.csv
#### Get list of users in specified OU and children
$gam config csv_output_header_force "primaryEmail" redirect csv ./users.csv append noheader ou_and_children_ns $OU print fields email
#### Add headers to calendar file
echo "primaryEmail,calendarId,accessRole" > ./calendars.csv
#### Get list of calendars, exclude classroom & personal.
$gam config csv_output_header_force "primaryEmail,calendarId,accessRole" csv_output_row_drop_filter "calendarId:regex:(.*@classroom\.google\.com|.*classroom.*@group\.calendar.\google\.com|family.*@group\.calendar.\google\.com)" csv_output_row_filter "'accessRole:regex:(owner)','calendarId:regex:(.*@group\.calendar\.google\.com)'" csv ./users.csv gam redirect csv ./calendars.csv append noheader user "~primaryEmail" print calendars fields id,accessRole
#### Add owner to all calendars
$gam csv ./calendars.csv gam calendar "~calendarId" add owner $calmaster sendnotifications false
#### Make calendars visible for CALMASTER
$gam csv ./calendars.csv gam user $calmaster add calendar "~calendarId" selected true
# Bonus: Find empty calendars!
#### Add headers to events files
echo "calendar,events" > ./events.csv
#### Get number of events per calendar
$gam csv ./calendars.csv gam calendar "~calendarId" show events countsonly >> ./events.csv
#### convert events to actual csv
sed -i 's/^Calendar: //g; s/Events://g; s/Event://g; s/ //g' ./events.csv
#### Create headers for empty-calendars file
echo "calendarId,eventCount" > ./empty-calendars.csv
#### Find empty calendars
sed '/,0$/!d' ./events.csv >> ./empty-calendars.csv
