Skip to content

Instantly share code, notes, and snippets.

@camerondavison
Created July 16, 2020 15:03
Show Gist options
  • Select an option

  • Save camerondavison/592e3ace8599834f43f767b8c0dfd715 to your computer and use it in GitHub Desktop.

Select an option

Save camerondavison/592e3ace8599834f43f767b8c0dfd715 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import structlog
log = structlog.get_logger()
def main():
SERVICE_ACCOUNT_FILE = "credentials.json"
SCOPES = [
"https://www.googleapis.com/auth/admin.directory.user.readonly",
"https://www.googleapis.com/auth/admin.directory.group.readonly",
]
delegated_user = "[email protected]"
credentials = ServiceAccountCredentials.from_json_keyfile_name(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
credentials = credentials.create_delegated(delegated_user)
service = build("admin", "directory_v1", credentials=credentials)
found_groups = []
n = 0
request = None
results = None
while True:
if request is None:
request = service.groups().list(customer="my_customer", orderBy="email",)
else:
request = service.groups().list_next(request, results)
if not request:
break
results = request.execute()
groups = results.get("groups", [])
for group in groups:
n += 1
if n % 50 == 0:
log.info(f"through {n} and found {len(found_groups)}")
member_count = int(group["directMembersCount"])
if member_count > 0:
found_groups.append(group["email"])
log.info(f"found {n} groups where {len(found_groups)} have members")
if __name__ == "__main__":
main()
@camerondavison
Copy link
Author

2020-07-16 10:02.01 through 50 and found 11
2020-07-16 10:02.01 through 100 and found 21
2020-07-16 10:02.01 through 150 and found 26
2020-07-16 10:02.01 through 200 and found 38
2020-07-16 10:02.01 through 250 and found 49
2020-07-16 10:02.01 through 300 and found 59
2020-07-16 10:02.01 through 350 and found 69
2020-07-16 10:02.01 through 400 and found 78
2020-07-16 10:02.02 through 450 and found 87
2020-07-16 10:02.02 through 500 and found 99
2020-07-16 10:02.02 through 550 and found 112
2020-07-16 10:02.02 through 600 and found 123
2020-07-16 10:02.03 through 650 and found 133
2020-07-16 10:02.03 through 700 and found 144
2020-07-16 10:02.03 through 750 and found 156
2020-07-16 10:02.03 through 800 and found 163
2020-07-16 10:02.03 through 850 and found 173
2020-07-16 10:02.03 through 900 and found 186
2020-07-16 10:02.03 through 950 and found 191
2020-07-16 10:02.03 through 1000 and found 203
2020-07-16 10:02.03 through 1050 and found 207
2020-07-16 10:02.03 through 1100 and found 214
2020-07-16 10:02.03 through 1150 and found 223
2020-07-16 10:02.03 through 1200 and found 241
2020-07-16 10:02.04 through 1250 and found 253
2020-07-16 10:02.04 through 1300 and found 274
2020-07-16 10:02.04 through 1350 and found 286
2020-07-16 10:02.04 through 1400 and found 298
2020-07-16 10:02.04 through 1450 and found 309
2020-07-16 10:02.04 through 1500 and found 325
2020-07-16 10:02.04 through 1550 and found 342
2020-07-16 10:02.04 through 1600 and found 349
2020-07-16 10:02.04 through 1650 and found 366
2020-07-16 10:02.04 through 1700 and found 377
2020-07-16 10:02.04 through 1750 and found 383
2020-07-16 10:02.04 through 1800 and found 398
2020-07-16 10:02.05 through 1850 and found 408
2020-07-16 10:02.05 through 1900 and found 413
2020-07-16 10:02.05 through 1950 and found 420
2020-07-16 10:02.05 through 2000 and found 433
2020-07-16 10:02.05 through 2050 and found 448
2020-07-16 10:02.05 through 2100 and found 460
2020-07-16 10:02.05 through 2150 and found 468
2020-07-16 10:02.05 through 2200 and found 476
2020-07-16 10:02.05 through 2250 and found 488
2020-07-16 10:02.05 through 2300 and found 505
2020-07-16 10:02.05 through 2350 and found 519
2020-07-16 10:02.05 through 2400 and found 525
2020-07-16 10:02.06 through 2450 and found 536
2020-07-16 10:02.06 through 2500 and found 545
2020-07-16 10:02.06 through 2550 and found 555
2020-07-16 10:02.06 through 2600 and found 563
2020-07-16 10:02.06 through 2650 and found 569
2020-07-16 10:02.06 through 2700 and found 579
2020-07-16 10:02.06 through 2750 and found 589
2020-07-16 10:02.06 through 2800 and found 604
2020-07-16 10:02.06 through 2850 and found 612
2020-07-16 10:02.06 through 2900 and found 620
2020-07-16 10:02.06 through 2950 and found 631
2020-07-16 10:02.06 through 3000 and found 636
2020-07-16 10:02.07 through 3050 and found 648
2020-07-16 10:02.07 through 3100 and found 660
2020-07-16 10:02.07 through 3150 and found 664
2020-07-16 10:02.07 through 3200 and found 673
2020-07-16 10:02.08 through 3250 and found 683
2020-07-16 10:02.08 through 3300 and found 689
2020-07-16 10:02.08 through 3350 and found 696
2020-07-16 10:02.08 through 3400 and found 704
2020-07-16 10:02.09 through 3450 and found 718
2020-07-16 10:02.09 through 3500 and found 728
2020-07-16 10:02.09 through 3550 and found 737
2020-07-16 10:02.09 through 3600 and found 749
2020-07-16 10:02.09 through 3650 and found 758
2020-07-16 10:02.09 through 3700 and found 765
2020-07-16 10:02.09 through 3750 and found 778
2020-07-16 10:02.09 through 3800 and found 787
2020-07-16 10:02.11 through 3850 and found 794
2020-07-16 10:02.11 through 3900 and found 807
2020-07-16 10:02.11 through 3950 and found 821
2020-07-16 10:02.11 through 4000 and found 831
2020-07-16 10:02.12 through 4050 and found 843
2020-07-16 10:02.12 through 4100 and found 849
2020-07-16 10:02.12 through 4150 and found 860
2020-07-16 10:02.12 through 4200 and found 875
2020-07-16 10:02.12 through 4250 and found 895
2020-07-16 10:02.12 through 4300 and found 903
2020-07-16 10:02.12 through 4350 and found 914
2020-07-16 10:02.12 through 4400 and found 927
2020-07-16 10:02.12 through 4450 and found 934
2020-07-16 10:02.12 through 4500 and found 938
2020-07-16 10:02.12 through 4550 and found 944
2020-07-16 10:02.12 through 4600 and found 953
2020-07-16 10:02.13 through 4650 and found 968
2020-07-16 10:02.13 through 4700 and found 984
2020-07-16 10:02.13 through 4750 and found 997
2020-07-16 10:02.13 through 4800 and found 1009
2020-07-16 10:02.13 through 4850 and found 1017
2020-07-16 10:02.13 through 4900 and found 1029
2020-07-16 10:02.13 through 4950 and found 1044
2020-07-16 10:02.13 through 5000 and found 1054
2020-07-16 10:02.13 through 5050 and found 1069
2020-07-16 10:02.13 through 5100 and found 1082
2020-07-16 10:02.13 through 5150 and found 1089
2020-07-16 10:02.13 through 5200 and found 1099
2020-07-16 10:02.14 through 5250 and found 1106
2020-07-16 10:02.14 through 5300 and found 1116
2020-07-16 10:02.14 through 5350 and found 1124
2020-07-16 10:02.14 found 5363 groups where 1129 have members

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment