|
| 1 | +function Invoke-AddGroupTeam { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + .ROLE |
| 6 | + Identity.Group.ReadWrite |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $APIName = $Request.Params.CIPPEndpoint |
| 12 | + $TenantFilter = $Request.Body.TenantFilter |
| 13 | + $GroupId = $Request.Body.GroupId |
| 14 | + |
| 15 | + $Results = [System.Collections.Generic.List[string]]@() |
| 16 | + |
| 17 | + try { |
| 18 | + # Default team settings - can be customized via request body |
| 19 | + $TeamSettings = if ($Request.Body.TeamSettings) { |
| 20 | + $Request.Body.TeamSettings |
| 21 | + } else { |
| 22 | + @{ |
| 23 | + memberSettings = @{ |
| 24 | + allowCreatePrivateChannels = $true |
| 25 | + allowCreateUpdateChannels = $true |
| 26 | + } |
| 27 | + messagingSettings = @{ |
| 28 | + allowUserEditMessages = $true |
| 29 | + allowUserDeleteMessages = $true |
| 30 | + } |
| 31 | + funSettings = @{ |
| 32 | + allowGiphy = $true |
| 33 | + giphyContentRating = 'strict' |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + # Create team from group using PUT request |
| 39 | + $GraphParams = @{ |
| 40 | + uri = "https://graph.microsoft.com/beta/groups/$GroupId/team" |
| 41 | + tenantid = $TenantFilter |
| 42 | + type = 'PUT' |
| 43 | + body = ($TeamSettings | ConvertTo-Json -Depth 10) |
| 44 | + AsApp = $true |
| 45 | + } |
| 46 | + $null = New-GraphPOSTRequest @GraphParams |
| 47 | + |
| 48 | + $Results.Add("Successfully created team from group $GroupId") |
| 49 | + Write-LogMessage -API $APIName -tenant $TenantFilter -message "Created team from group $GroupId" -Sev 'Info' |
| 50 | + } catch { |
| 51 | + $ErrorMessage = Get-CippException -Exception $_ |
| 52 | + $RawMessage = $_.Exception.Message |
| 53 | + |
| 54 | + # Determine if this is a likely replication delay 404 (exclude owner/membership related 404s) |
| 55 | + $Is404 = ($RawMessage -match '404|Not Found' -or $ErrorMessage.NormalizedError -match '404|Not Found') |
| 56 | + $IsOwnerRelated = ($RawMessage -match 'owner' -or $ErrorMessage.NormalizedError -match 'owner') |
| 57 | + $IsMembershipRelated = ($RawMessage -match 'member' -or $ErrorMessage.NormalizedError -match 'member') |
| 58 | + |
| 59 | + $IsReplicationDelay = $Is404 -and -not ($IsOwnerRelated -or $IsMembershipRelated) |
| 60 | + |
| 61 | + if ($IsReplicationDelay) { |
| 62 | + $Results.Add('Failed to create team: The group may have been created too recently. If it was created less than 15 minutes ago, wait and retry. Groups need time to fully replicate before a team can be created.') |
| 63 | + Write-LogMessage -API $APIName -tenant $TenantFilter -message "Failed to create team from group $GroupId - probable replication delay (404). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage |
| 64 | + } else { |
| 65 | + $Results.Add("Failed to create team: $($ErrorMessage.NormalizedError)") |
| 66 | + Write-LogMessage -API $APIName -tenant $TenantFilter -message "Failed to create team from group $GroupId. Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + $Body = @{ |
| 71 | + Results = @($Results) |
| 72 | + } |
| 73 | + |
| 74 | + return ([HttpResponseContext]@{ |
| 75 | + StatusCode = [HttpStatusCode]::OK |
| 76 | + Body = $Body |
| 77 | + }) |
| 78 | +} |
0 commit comments