Created
October 8, 2021 14:22
-
-
Save mhupfauer/23391c9402f7c291d12c8bb12b4d4dcc to your computer and use it in GitHub Desktop.
Send Microsoft Teams Message via PowerShell GraphAPI to all users based on regex pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-BroadcastMessage { | |
param( | |
$user_pattern, | |
$sender_id, | |
$message | |
) | |
Connect-MgGraph "Chat.ReadWrite","Chat.ReadWrite","User.Read.All" | |
$contacts = Get-MgUser -All:$true | Where-Object {$_.userprincipalname -like $user_pattern} | |
foreach($c in $contacts) | |
{ | |
$odata = @" | |
{ | |
"chatType": "oneOnOne", | |
"members": [ | |
{ | |
"@odata.type": "#microsoft.graph.aadUserConversationMember", | |
"roles": ["owner"], | |
"user@odata.bind": "https://graph.microsoft.com/beta/users('$($sender_id)')" | |
}, | |
{ | |
"@odata.type": "#microsoft.graph.aadUserConversationMember", | |
"roles": ["owner"], | |
"user@odata.bind": "https://graph.microsoft.com/beta/users('$($c.Id)')" | |
} | |
] | |
} | |
"@ | |
$chat = New-MgChat -BodyParameter $odata | |
New-MgChatMessage -ChatId $chat.Id -Body @" > $null | |
{ | |
"content": "$($message)" | |
} | |
"@ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment