|
| 1 | +function Invoke-CIPPStandardTeamsExternalChatWithAnyone { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Internal |
| 5 | + .COMPONENT |
| 6 | + (APIName) TeamsExternalChatWithAnyone |
| 7 | + .SYNOPSIS |
| 8 | + (Label) Control Teams "Chat with anyone" feature |
| 9 | + .DESCRIPTION |
| 10 | + (Helptext) Manages whether users can initiate Microsoft Teams chats with any email address, inviting non-Teams users as guests via email. |
| 11 | + (DocsDescription) Manages the UseB2BInvitesToAddExternalUsers setting on the global Teams messaging policy. When enabled, users can start chats with any email address and external recipients receive an invitation to join as guests. Disabling the setting prevents external email-based chats from being created, keeping conversations restricted to approved collaborators. |
| 12 | + .NOTES |
| 13 | + CAT |
| 14 | + Teams Standards |
| 15 | + TAG |
| 16 | + EXECUTIVETEXT |
| 17 | + Controls whether employees can start Microsoft Teams chats with anyone using just their email address. Turning this off keeps chat conversations restricted to internal users and pre-approved guests, reducing the risk of data exposure through unexpected external invitations. |
| 18 | + ADDEDCOMPONENT |
| 19 | + {"type":"switch","name":"standards.TeamsExternalChatWithAnyone.UseB2BInvitesToAddExternalUsers","label":"Allow chatting with anyone via email","defaultValue":false} |
| 20 | + IMPACT |
| 21 | + Medium Impact |
| 22 | + ADDEDDATE |
| 23 | + 2025-11-03 |
| 24 | + POWERSHELLEQUIVALENT |
| 25 | + Set-CsTeamsMessagingPolicy -Identity Global -UseB2BInvitesToAddExternalUsers $false |
| 26 | + RECOMMENDEDBY |
| 27 | + "CIPP" |
| 28 | + UPDATECOMMENTBLOCK |
| 29 | + Run the Tools\Update-StandardsComments.ps1 script to update this comment block |
| 30 | + .LINK |
| 31 | + https://docs.cipp.app/user-documentation/tenant/standards/list-standards |
| 32 | + #> |
| 33 | + |
| 34 | + param($Tenant, $Settings) |
| 35 | + $TestResult = Test-CIPPStandardLicense -StandardName 'TeamsExternalChatWithAnyone' -TenantFilter $Tenant -RequiredCapabilities @('MCOSTANDARD', 'MCOEV', 'MCOIMP', 'TEAMS1', 'Teams_Room_Standard') |
| 36 | + |
| 37 | + if ($TestResult -eq $false) { |
| 38 | + Write-Host "We're exiting as the correct license is not present for this standard." |
| 39 | + return $true |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + $CurrentState = New-TeamsRequest -TenantFilter $Tenant -Cmdlet 'Get-CsTeamsMessagingPolicy' -CmdParams @{ Identity = 'Global' } | Select-Object -Property Identity, UseB2BInvitesToAddExternalUsers |
| 44 | + } catch { |
| 45 | + $ErrorMessage = Get-CippException -Exception $_ |
| 46 | + Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the Teams external chat state for $Tenant. Error: $($ErrorMessage.NormalizedError)" -Sev Error -LogData $ErrorMessage |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + # Set default to Disabled if not specified. Should not be possible without some serious misconfiguration via the API |
| 51 | + $Settings.UseB2BInvitesToAddExternalUsers ??= $false |
| 52 | + $DesiredState = [System.Convert]::ToBoolean($Settings.UseB2BInvitesToAddExternalUsers) |
| 53 | + $StateIsCorrect = ($CurrentState.UseB2BInvitesToAddExternalUsers -eq $DesiredState) |
| 54 | + |
| 55 | + if ($Settings.remediate -eq $true) { |
| 56 | + if ($StateIsCorrect -eq $true) { |
| 57 | + Write-LogMessage -API 'Standards' -tenant $Tenant -message "Teams external chat with anyone setting already set to: $DesiredState" -sev Info |
| 58 | + } else { |
| 59 | + $cmdParams = @{ |
| 60 | + Identity = 'Global' |
| 61 | + UseB2BInvitesToAddExternalUsers = $DesiredState |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + $null = New-TeamsRequest -TenantFilter $Tenant -Cmdlet 'Set-CsTeamsMessagingPolicy' -CmdParams $cmdParams |
| 66 | + Write-LogMessage -API 'Standards' -tenant $Tenant -message "Successfully updated Teams external chat with anyone setting to UseB2BInvitesToAddExternalUsers: $DesiredState" -sev Info |
| 67 | + } catch { |
| 68 | + $ErrorMessage = Get-CippException -Exception $_ |
| 69 | + Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to configure Teams external chat with anyone setting. Error: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if ($Settings.alert -eq $true) { |
| 75 | + if ($StateIsCorrect -eq $true) { |
| 76 | + Write-LogMessage -API 'Standards' -tenant $Tenant -message "Teams external chat setting is configured correctly as: $DesiredState" -sev Info |
| 77 | + } else { |
| 78 | + Write-StandardsAlert -message 'Teams external chat setting is not configured correctly.' -object $CurrentState -tenant $Tenant -standardName 'TeamsExternalChatWithAnyone' -standardId $Settings.standardId |
| 79 | + Write-LogMessage -API 'Standards' -tenant $Tenant -message 'Teams external chat setting is not configured correctly.' -sev Info |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if ($Settings.report -eq $true) { |
| 84 | + Add-CIPPBPAField -FieldName 'TeamsExternalChatWithAnyone' -FieldValue $StateIsCorrect -StoreAs bool -Tenant $Tenant |
| 85 | + |
| 86 | + if ($StateIsCorrect) { |
| 87 | + $FieldValue = $true |
| 88 | + } else { |
| 89 | + $FieldValue = $CurrentState |
| 90 | + } |
| 91 | + Set-CIPPStandardsCompareField -FieldName 'standards.TeamsExternalChatWithAnyone' -FieldValue $FieldValue -Tenant $Tenant |
| 92 | + } |
| 93 | +} |
0 commit comments