Forum Discussion

PS-Rainer's avatar
PS-Rainer
Copper Contributor
Sep 16, 2022

Add user to teams chat

Hi, 
I want to add an exising user to a team chat with PowerShell, but I can't find a working solution in the documentation or on google, hopefulle someone can help here:

I found following commands, but non of them I get to work:
New-MgUserChatMember --> Throws error when using
Add-MgUserChatMember --> Throws error when using
Add-MgChatMember --> I don't know how to build BODYPARAMETERS

 

To my script:

first I installed all modules of Microsoft-Graph:

 

 

Install-Module Microsoft.Graph
Find-Module Microsoft.Graph.* | Install-Module

 

 

 

Then I connected to MS-Graph:

 

 

Connect-MgGraph

 

 

 

Here I get the user which should be added to the chat(s):

 

 

$user = Get-MgUser -Filter "UserPrincipalName eq 'email address removed for privacy reasons'"

 

 

 

Now get the chat groups where to add the user, and add it with the commands above

 

 

$groups = Get-MgChat -Filter "ChatType eq 'group'" | Where-Object Topic -in $chatgroups
foreach ($group in $groups) {
    New-MgUserChatMember -UserId $user.Id -ChatId $group.Id
    Add-MgUserChatMember -UserId $user.Id -ChatId $group.Id
}

 

 

 

But when I run this I get following error messages:

 

 

New-MgUserChatMember : Requested API is not supported. Please check the path.
At N:\Administration\IT\60 Tools\PowerShell\MS-Teams\SyncMSTeamsChatGroupMembers.ps1:93 char:25
+ ...               New-MgUserChatMember -UserId $user.Id -ChatId $group.Id
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: ({ UserId = 1fe3...rsationMember }:<>f__AnonymousType197`3) [New-MgUserChatMember_CreateExpanded], RestException` 
   1
    + FullyQualifiedErrorId : NotFound,Microsoft.Graph.PowerShell.Cmdlets.NewMgUserChatMember_CreateExpanded

Add-MgUserChatMember : 404 page not found
At N:\Administration\IT\60 Tools\PowerShell\MS-Teams\SyncMSTeamsChatGroupMembers.ps1:94 char:25
+ ...               Add-MgUserChatMember -UserId $user.Id -ChatId $group.Id
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: ({ UserId = 1fe3...ionJsonSchema }:<>f__AnonymousType0`3) [Add-MgUserChatMember_AddExpanded1], RestException`1
    + FullyQualifiedErrorId : UnknownError,Microsoft.Graph.PowerShell.Cmdlets.AddMgUserChatMember_AddExpanded1

 

 

 

I also tried to get the command Add-MgChatMember to work, but I don't know how to prepare the BODYPARAMS-Attribute, I tried different settings, but always got: AclCheckFailed-Error, seams missing permissions, but I can access the chat and the user info, but can't update it? Is there a special authentication needed. How to implement this?

I would appreciate any help, thanks in advance.
Rainer

  • raindropsdev's avatar
    raindropsdev
    Iron Contributor

    PS-Rainer 

    When taking a look directly at the API call used it appears that the command is New-MgChatMember:
    https://learn.microsoft.com/en-us/graph/api/chat-post-members?view=graph-rest-1.0&tabs=powershell

     

     

    Import-Module Microsoft.Graph.Teams
    
    $params = @{
    	"@odata.type" = "#microsoft.graph.aadUserConversationMember"
    	"email address removed for privacy reasons" = "https://graph.microsoft.com/v1.0/users/8b081ef6-4792-4def-b2c9-c363a1bf41d5"
    	VisibleHistoryStartDateTime = [System.DateTime]::Parse("2019-04-18T23:51:43.255Z")
    	Roles = @(
    		"owner"
    	)
    }
    
    New-MgChatMember -ChatId $chatId -BodyParameter $params

     

     

    Uh, it appears that techcommunity breaks stuff with the email protection, I recommend you look at the code directly in Microsoft Docs

     

Resources