Can you run more than 1 GAM command in a single script?

694 views
Skip to first unread message

Mark Morris

unread,
May 6, 2014, 11:48:46 AM5/6/14
to google-ap...@googlegroups.com
I have a large batch of groups that I would like to create and then add the users to them, all in the 1 script.

I have a CSV that contains the groups email, name, and a unc path to another CSV that has the unique user list for that group.

I can easily run two different scripts, one to create the groups and then another to go to each group and add the users.  However, I would like to know if there is a way to achieve this in a single script.  When I try to do this, the script execution ends after the first GAM command completes (which is creating the group) and will not move on to the next GAM command to add the users.  I am executing the scripts in Power Shell, since it is what I am familiar with.  I am pretty novice at scripting, so if I am trying to do something or missing something dumb and obvious, just tell me.  Below is my code.

(Single Script to create the groups - Works just fine!)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
$groupinfo = Import-csv "C:\GAM\Scripts\Groups Script\Groups List.csv"
foreach ($groupentry in $groupinfo)
{
c:\gam\gam.exe create group $($groupentry.email) name $($groupentry.name)
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

(Single script to add the users - Works just fine!)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
$groupinfo = Import-csv "C:\GAM\Scripts\Groups Script\Groups List.csv"
foreach ($groupentry in $groupinfo)
{$userslist = import-csv $($groupentry.usersfile)
foreach ($userentry in $userslist)
{
c:\gam\gam.exe update group $($groupentry.email) add member $($userentry.users)
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

(My attempt at combining them.  When I run, it works up to creating the group, and then spits the rest out in plain text without any error)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
$groupinfo = Import-csv "C:\GAM\Scripts\Groups Script\Groups List.csv"
foreach ($groupentry in $groupinfo)
{
c:\gam\gam.exe create group $($groupentry.email) name $($groupentry.name)
{$userslist = import-csv $($groupentry.usersfile)
foreach ($userentry in $userslist)
{
c:\gam\gam.exe update group $($groupentry.email) add member $($userentry.users)
}
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Greg Harry

unread,
Sep 12, 2017, 10:57:25 AM9/12/17
to GAM for G Suite
Hi Mark,

This reply is probably 3 years too late but hopefully someone else can gain value from this thread.  This is possible by adding ";" between the commands you wish to run in the script for example:

$list = Import-CSV example_group.csv
for each _$entry in $list)
   {
      .\gam.exe create group $($entry.group)
   };

Next command....The commands will run sequentially so you can't have an update command first if the group doesn't exist yet.  I hope this helps.
Reply all
Reply to author
Forward
0 new messages