Greetings All,
I have been tasked with the creation of 5000+ Office 365 groups. The settings for which will come from JSON files generated by a Unix mailsystem. The current issue is with populating the membership. In the JSON files where the members are listed, some of the members do not exist, so in testing, when I attempt to populate the membership with the add-unifiedgrouplinks cmdlet, if even one object is bad, it skips that group. Ideally, I’m looking to loop through the members list before running add-unifiedgrouplinks and filter out any that do not exist and then feed that updated list into the function that creates the group, but I am clearly just not grasping the logic of how to accomplish this task. I am hoping someone might be able to guide me in the right direction. Here is what I have so far (admittedly, some of my choices for the test function are probably way off base):
# First, here is a sample of JSON data: { "AccessType": "Private", "AutosubscribeNewMembers": true, "DisplayName": "testgroup1", "PrimarySMTPAddress": "testgroup1@nowhere.local", "HiddenFromAddressListsEnabled": true, "HiddenGroupMembershipEnabled": true, "ManagedBy": "listowner", "Members": [ "testuser1", “listmaster”, "realaddress@grealdomain.local", "bogusaddress" ] } In the above list of members, all but bogusaddress exist. # VARIABLES: # Variable that cycles through each .json file and converts it to a Powershell object $jsonFiles = Get-ChildItem -path "c:\tmp\json" -filter *.json | get-content -raw $allobjects = $jsonFiles | convertfrom-json $testMembers = ForEach-Object{$allobjects.members}{select $_} # Set path for log files: $logPath = "c:\tmp\logs" # FUNCTIONS: function add-GroupMembers { [CmdletBinding()] Param( [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] $InputObject ) Process { try { write-host "Adding Group Members: gr-$($InputObject.DisplayName)" if($InputObject.managedby -notin $InputObject.members){ Write-Host "Adding $($InputObject.managedby) to gr-$($InputObject.DisplayName) before setting as an owner" Add-UnifiedGroupLinks -Identity "gr-$($InputObject.DisplayName)" ` -LinkType members ` -Links $InputObject.managedby Add-UnifiedGroupLinks -Identity "gr-$($InputObject.DisplayName)" ` -LinkType members ` -Links $InputObject.members # USED TO VALIDATE ABOVE WORKED $groupMembers = Get-UnifiedGroupLinks -Identity "gr-$($InputObject.DisplayName)" -LinkType members | ` select name -ExpandProperty name write-output "$($timeFull):`r`nGroup: gr-$($InputObject.Displayname)`r`nMembers: $($groupMembers)`r`n" | ` out-file -Append "$($logPath)\$($timeShort)_add-GroupMembers.log" } else{ Add-UnifiedGroupLinks -Identity "gr-$($InputObject.DisplayName)" ` -LinkType members ` -Links $InputObject.members # USED TO VALIDATE ABOVE WORKED $groupMembers = Get-UnifiedGroupLinks -Identity "gr-$($InputObject.DisplayName)" -LinkType members | ` select name -ExpandProperty name write-output "$($timeFull):`r`nGroup: gr-$($InputObject.Displayname)`r`nMembers: $($groupMembers)`r`n" | ` out-file -Append "$($logPath)\$($timeShort)_add-GroupMembers.log" } } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName write-output "$($timeFull): Error trying to add members to group: gr-$($InputObject.DisplayName);`r`nError Message: $($ErrorMessage)" `r`n | ` out-file -append -filepath "$($logPath)\$($timeShort)_add-GroupMembers_error.log" } } } With legitimate data, this function works when I run: $allobjects | add-GroupMembers I've tried adding an additional function with an additional variable ($testMembers) just to test logic: function test-GroupMembers { [CmdletBinding()] Param( [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] $InputObject, [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] $badMember ) Process { write-host "Testing Group Members: gr-$($InputObject.DisplayName)" Get-AzureADUser -filter "DisplayName eq '$($badMember)'" if(-not $null){ # TESTING ONLY write-host $badMember } else{ #TESTING ONLY write-host “Command failed” } } } $allobjects | test-GroupMembers -badMember $testmembers
I must admit I am a bit lost at this point. The add-GroupMembers function works ok provided there are no bad entries in the Members section. Also, I am using get-azureaduser because the member can be eiter an internal user or external recipient.
Thank you in advance for any assistance that can be provided.