Hijacking Google Calendars - A Script

Hijacking Google Calendars - A Script
Photo by Monica Sauro / Unsplash

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 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, remove 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.

# Let's dump a list of ALL calendars for all users in our domain into a CSV file.
gam all users print calendars > $CSVPATH

# Now we'll import that CSV file into an array.
$calendars = import-csv -path $CSVPATH

<# Now we will filter that list and:
1) Keep only shared calendars (Personal Calendars)
2) Remove any Google Classroom Calendars
3) Keep only Calendars owned by our users
4) Remove any calendars already owned by our designated user. #>

$calendars = $calendars | Where-Object {`
($_.id -like "*group.calendar.google.com")`
-and ($_.id -notlike "*classroom*")`
-and ($_.accessRole -like "owner")`
-and ($_.primaryEmail -notlike $CALMASTER) } 

# Loop through the list of Calendars
foreach ($calendar in $calendars) {
    # Add our designated user as an owner.
    gam calendar $calendar.id add owner $CALMASTER sendnotifications false
    # Make the calendar visible for our user
    gam user $CALMASTER add calendar $CALMASTER.id selected false

Joshua Martin / GAM-Bulk-Calendar-Wrangler · GitLab
GitLab.com

Full Code available here.