I’ve spent the morning retrieving all of the users from a bunch of AAD Groups. The trouble was that groups were also members of the groups, so it was necessary to get the member of those groups as well. Actually, this was no trouble at all; it was a case of looping through and writing the users to a csv and looping through the sub-groups to get the users and groups etc etc. What was really troublesome was that there was a whole lot of groups that there were cyclical memberships; where Group A was a member of Group B, and Group B was a member of Group C, and Group C was a member of Group A, and so I was stuck in an infinite loop of retrieving the same groups. So this explains passing an array of groups that have already been processed.
Finally because I’m using this to email a bunch of people Ive removed the duplicates.
Anyway it might be useful to someone somewhere presumably so here it is in beautiful gist form.