Skip to content

Instantly share code, notes, and snippets.

@mhupfauer
Created October 8, 2021 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhupfauer/23391c9402f7c291d12c8bb12b4d4dcc to your computer and use it in GitHub Desktop.
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
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