bulk add users to google group without using a csv

2,260 views
Skip to first unread message

Avrohom Eliezer Friedman

unread,
Aug 17, 2018, 10:56:22 AM8/17/18
to google-ap...@googlegroups.com
Is there a way to bulk add users to a google group without using a csv?

Sounds petty but I want to add users to a google group but I really don't want to upload a csv to the cloudshell it's running off each time I have to do this. Is there a way I can cut and paste the email addresses into a command?



-----
Avrohom Eliezer Friedman
IT Director

Yeshiva Toras Chaim Toras Emes
305-944-5344 x222 (o)
305-494-2055 (cell)
aefri...@ytcteam.org

✉ Kevin Melillo

unread,
Aug 17, 2018, 11:09:14 AM8/17/18
to google-ap...@googlegroups.com
You can cut\paste.  Just use nano, or vi or another editor.  

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/CAKPyCc4O4ZEgeaYxcJv50oPjSbY1QxFO5%2BC3mYPJOJo_kWB_Rw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
Kevin Melillo
Electronic Communications Analyst
Information Technology
445 Hoes Lane
Piscataway, NJ 08854

Phone:732-465-6609 | Mobile: 732-609-4331

Sam Woodhouse

unread,
Aug 17, 2018, 11:20:06 AM8/17/18
to GAM for G Suite
Hi Avrohom,

You can pipe commands into another command to avoid uploading a CSV, assuming you can find the correct subsection of users using "gam print users query ...." you can pipe that into a group update command.

For example 

gam print users query "orgunitpath='/org/path'" | gam csv - gam update group groupexample@domain.com add member ~email


This does assume you're able to filter the exact subsection of users using the print command.


Scroll to the section starting in "Note also that you can use shell pipes and a special CSV file called..."

Avrohom Eliezer Friedman

unread,
Aug 17, 2018, 11:39:43 AM8/17/18
to google-ap...@googlegroups.com
Kevin - I guess I could do that - but it's just annoying - I was hoping for a single command 
Sam - they are external email addresses


Thanks



-----
Avrohom Eliezer Friedman
IT Director

Yeshiva Toras Chaim Toras Emes
305-944-5344 x222 (o)
305-494-2055 (cell)
aefri...@ytcteam.org

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

Bradley Stinson

unread,
Aug 17, 2018, 11:50:59 AM8/17/18
to google-ap...@googlegroups.com
I dont think there is going to be a single line to do it. You can create your csv from the cloudshell command line and then edit it like kevin said to put data into it and then run your bulk operation gam command. 
touch myNewSheet.csv
vim myNewSheet.csv


I would probably just construct my gam commands from the local spreadsheet with concat formulas and put a semi colon at the end of each command then copy and paste the whole lot of them into the terminal at once.


Ross Scroggs

unread,
Aug 17, 2018, 12:46:55 PM8/17/18
to google-ap...@googlegroups.com

Avrohom Eliezer Friedman

unread,
Aug 17, 2018, 12:58:47 PM8/17/18
to google-ap...@googlegroups.com
THANK YOU! 



-----
Avrohom Eliezer Friedman
IT Director

Yeshiva Toras Chaim Toras Emes
305-944-5344 x222 (o)
305-494-2055 (cell)
aefri...@ytcteam.org

+KimNilsson

unread,
Aug 18, 2018, 5:46:32 AM8/18/18
to GAM for G Suite
Is there a max length of that command?
Meaning, how many new group members can I add, comma separated like that?

Ross Scroggs

unread,
Aug 18, 2018, 9:25:50 AM8/18/18
to google-ap...@googlegroups.com
KIm,

Gam certainly doesn't have a limit, I suppose any limit would come from your OS in processing the command line.

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

Peter Smulders

unread,
Aug 18, 2018, 5:44:26 PM8/18/18
to GAM for G Suite
Avrohom,
 
Gam certainly doesn't have a limit, I suppose any limit would come from your OS in processing the command line.

On Cloud Shell, the limit is upwards of 2 million bytes, so you are not likely to hit that. However, for ease of pasting, this syntax avoids any constraints and is easier to deal with (because you do not need to create the separating commas):

$ cat << EOF | gam csv - gam whatever something something
> [do your paste here]
> EOF

This presumes:
1) Your data is separated by newlines, i.e. like a list of e-mail addresses in a single column copied out of an Excel sheet.
2) The first line is the correct CSV header.

You can avoid adding the csv header with a bit more trickery, like this:

$ (echo EmailAddress; cat << EOF) | gam csv - gam whatever something ~EmailAddress something
> [do your paste here]
> EOF

(note the brackets)

This way, you can have your command be 'contained' i.e. it only wants data to do something and you do not have to prepare the data to 'match' the command (by having the correct CSV header).

Normally, you can get earlier commands back through the shell's history features. My defaults on Cloud Shell seem to have cmdhist and lithist enabled. If you do this (maybe in your .bash_profile):

shopt -u cmdhist

all multiline commands will be saved as individual commands. Thus, you can do Ctrl+r followed by a few characters and get to your magic command. That said, it will take every e-mail address as a command, so you will need to set your history length to a very, very high number.

--peter

Ross Scroggs

unread,
Aug 18, 2018, 5:57:39 PM8/18/18
to google-ap...@googlegroups.com
Peter,

Here is the command I posted for Avrohom:
gam update group gr...@domain.com add member users "u...@domain.com,u...@otherdomain.com,..."

The list can be space separated:
gam update group gr...@domain.com add member users "u...@domain.com u...@otherdomain.com ..."

If you have a one column flat file with no header:
gam update group gr...@domain.com add member file Users.lst

If you have a multi column flat file with no header and some delimiter, e.g. comma
gam update group gr...@domain.com add member file Users.lst delimiter ","

In your example, there is one API call per user, in my examples there is one API call.


Ross
--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

Peter Smulders

unread,
Aug 18, 2018, 6:18:12 PM8/18/18
to GAM for G Suite
The list can be space separated:
gam update group gr...@domain.com add member users "u...@domain.com u...@otherdomain.com ..."

Even though I strongly suspect Avrohom cares a lot more about convenience than API calls :) I offer him (and everyone else) this happy medium, with both pasting ease and single API call performance for the case where data is in a single column with no heading (which is what life in Excel is all about.. :)

$ MY_CSV_FILE=$(mktemp) && cat << EOF > "${MY_CSV_FILE}" && gam update group your...@yourdomain.com add member file "${MY_CSV_FILE}" && rm "${MY_CSV_FILE}"
> [paste stuff from Excel]
> EOF

Everybody wins! :))

If I were Avrohom, I would maintain a text file with these commands and copy&paste them, followed by whatever list of emails need to be added today.


--peter

Peter Smulders

unread,
Aug 18, 2018, 6:30:30 PM8/18/18
to GAM for G Suite
Oh, I just stumbled onto a waaay better syntax. Apparently, all commands that can use '-' as a substitute for STDIN instead of a file name, can also handle so-called 'here-documents' (which is what the '<<' is for). So you can just do this:

$ gam update group your...@example.com add member file - << EOF
> [paste here]
> EOF

and you're done!

--peter

Ross Scroggs

unread,
Aug 18, 2018, 6:30:39 PM8/18/18
to google-ap...@googlegroups.com
Peter,

Couldn't you also do this (all Windows users move on, nothing to see here)
gam update group your...@yourdomain.com add member users "
[paste stuff from Excel]
"
Hit return after both "

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

Peter Smulders

unread,
Aug 18, 2018, 6:42:46 PM8/18/18
to GAM for G Suite
Couldn't you also do this (all Windows users move on, nothing to see here)
gam update group your...@yourdomain.com add member users "
[paste stuff from Excel]
"
Hit return after both "

That very much depends on how gam (or Python) splits arguments into words. You would be handing gam a large string with embedded newlines. If it automatically splits that into words (i.e. strings without spaces) then yes, but from a UNIX 'gut feeling' I would expect newlines to be treated differently from spaces/tabs/whatnot. In your example, only the first line would be processed, which is empty.

Judging from your example syntax for the multi-column some-separator, I suspect the argument splitter actually will eat anything, splitting lines the best if knows how to, until End-Of-File is reached.

So concluding: yes, that would very likely work, but from a UNIX perspective, it really ought not to do that. What I mean is that in this case gam(adv-x) is not following UNIX conventions. (value that as you see fit).

--peter


Ross Scroggs

unread,
Aug 18, 2018, 7:06:21 PM8/18/18
to google-ap...@googlegroups.com
Peter,

Both Standard Gam and Advanced Gam work the same way when reading lists; commas are converted to spaces and then the string is split on any whitespace (newlines, carriage returns, tabs, spaces)
$ gam update group testgroup1 add member users "      
testuser1
testuser2 
"
['/Users/Ross/Documents/GoogleApps/GAM/gamadvx.py', 'update', 'group', 'testgroup1', 'add', 'member', 'users', '\ntestuser1\ntestuser2\n']
Group: testg...@rdschool.org, Add 2 Members
  Group: testg...@rdschool.org, Member: test...@rdschool.org, Add Failed: Duplicate, already a Member (1/2)
  Group: testg...@rdschool.org, Member: test...@rdschool.org, Add Failed: Duplicate, already a Member (2/2)

I'm not quite sure I follow your argument here:
So concluding: yes, that would very likely work, but from a UNIX perspective, it really ought not to do that. What I mean is that in this case gam(adv-x) is not following UNIX conventions. (value that as you see fit).
Gam is handed a single argument in a context where a list is allowed, so it splits the argument on whitespace; what Unix convention says that this is not the correct approach? 

Ross

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

Peter Smulders

unread,
Aug 19, 2018, 5:16:07 AM8/19/18
to GAM for G Suite
Hi Ross --

Gam is handed a single argument in a context where a list is allowed, so it splits the argument on whitespace; what Unix convention says that this is not the correct approach?

I gave it some more thought and I think I am off-base here. The shell splits arguments into words on the Internal Field Seperator. Default value for IFS (according to the bash man page): '<space><tab><newline>'. I guess gam is in good company there.

The reason this irks me is, I think, that most of what I do has to do with text processing. Awk, sed, head, tail, cut, grep and looping in the shell (not the actual for and while, but the implied read) all are very aware of newlines. That does not generally apply to all of UNIX, nor should it apply to gam. So ... my bad.

In the wider context, the reason I brought it up is that I think that predictability is worthwhile property of the interface of any program, i.e. the syntax to use. For instance: most, if not all, GNU UNIX utilities conform to the convention that parameters can be supplied in any given order, with a single dash for the short form ('-h') and double dashes for the equivalent long form ('--help'). gam uses no dashes and for a large part the order of the arguments matter. That said, the same applies to ifconfig.

Furthermore, I don't think the design considerations for gam ever included 'act like a GNU utility' so my comments on syntax in general should be taken as nothing more than a tentative wish.

--peter

Avrohom Eliezer Friedman

unread,
Aug 24, 2018, 11:22:17 AM8/24/18
to google-ap...@googlegroups.com
So I got this error message

image.png

image.png
-----
Avrohom Eliezer Friedman
IT Director

Yeshiva Toras Chaim Toras Emes
305-944-5344 x222 (o)
305-494-2055 (cell)
aefri...@ytcteam.org
On Fri, Aug 17, 2018 at 12:46 PM Ross Scroggs <ross.s...@gmail.com> wrote:

Avrohom Eliezer Friedman

unread,
Aug 24, 2018, 11:25:39 AM8/24/18
to google-ap...@googlegroups.com
If I run it without quotes it works perfectly



-----
Avrohom Eliezer Friedman
IT Director

Yeshiva Toras Chaim Toras Emes
305-944-5344 x222 (o)
305-494-2055 (cell)
aefri...@ytcteam.org

Tom Morin

unread,
Aug 28, 2018, 5:06:17 AM8/28/18
to GAM for G Suite
you might like to look at a freeware scripting package called Autoit.  It is really easy to build scripts that can do so much. I've used it to launch a browser, send mouse clicks and export data, massage it how I want, create batch files that contain individual lines of code (exactly as you are looking for) and running those bat files to do various things. It is great for things that are repetitive in nature.  Just a thought for you.

Tom

Kim Nilsson

unread,
Aug 28, 2018, 6:14:15 AM8/28/18
to Google Apps Manager
You could also schedule GAM to download the contents of a Google Sheets to the Cloud Shell and use it as input.
Then you ony need to update the Sheet, and on next scheduled time GAM will grab the current info and run you command on it.


gam <UserTypeEntity> get drivefile <DriveFileEntity> [revision <DriveFileRevisionID>] [format <FileFormatList>]
        [targetfolder <FilePath>] [targetname <FileName>] [overwrite [<Boolean>]] [showprogress]

Ross, if you're reading this, I can't find the wiki information about this variable which is part of the download command.
I'm assuming we would want to download the file as a CSV.

format <FileFormatList>

One google search later and I'm now guessing that the info on this page is relevant?

/Kim
--
There is No Substitute!

Tom Morin

unread,
Aug 28, 2018, 10:01:09 AM8/28/18
to GAM for G Suite
I imagine you can. once you have the code line in dos.. You could use variables to prompt each time for username etc and do it that way.
With autoit you could build each line and execute them one by one without uploading a csv.
only other way is to build it excel..then copy and paste the full command line into the dis window and run it.  Easier would be to simply copy all those into notepad and save as a batch file and then just run it. All your cutting and pasting would be done in excel to get your individual code lines for each user. 
Half dozen one the other.
Tom

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

Ross Scroggs

unread,
Aug 28, 2018, 10:33:39 AM8/28/18
to google-ap...@googlegroups.com
Kim,

This is the correct page for the get drivefile command: https://github.com/taers232c/GAMADV-XTD/wiki/Users-Drive-Files#download-files
I've added the <FileFormat> and <FileFormatList> definitions.

Use format csv to download a Google Sheet as a csv file.

Ross

--
You received this message because you are subscribed to the Google Groups "GAM for G Suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To post to this group, send email to google-ap...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-manager.

For more options, visit https://groups.google.com/d/optout.


--
Reply all
Reply to author
Forward
0 new messages