如何使用 PowerShell 导出 MS Teams 聊天历史记录
在本文中,我们将了解如何使用 PowerShell 访问和导出 Microsoft Teams 聊天对话的历史记录。
团队聊天记录存储在隐藏的对话历史团队聊天共享邮箱中的文件夹,该文件夹是在您创建新的 Microsoft 365 组时自动创建的(这将立即创建 Teams 组、网站、SharePoint Online 库、Yammer 组等)。
您可以阻止 Microsoft 365 租户的用户创建新的 Teams 组。
但是,您无法使用 Outlook 或其他应用程序访问包含 Teams 聊天历史记录的受保护文件夹。您可以使用以下命令将 Exchange Online 邮箱的内容导出到 PST 文件内容搜索在安全与合规中心中,然后在 Outlook 中连接 PST 文件。但不太方便。使用 PowerShell 获取 Teams 聊天消息列表要容易得多。
要连接到 Microsoft 365 租户,我们将使用 Microsoft Graph API。
之前,我们向您展示了如何使用 PowerShell 和 Microsoft Graph API 向 MS Teams 聊天发送消息。
- 创建一个新的应用程序团队视图Azure 门户中的应用程序(Azure AD -> 应用程序注册 -> 新注册);
- 复制以下值:
应用程序(客户端)ID:your_app_ID
目录(租户)ID:your_tenant_ID - 转到 API 权限,单击 Microsoft Graph -> 应用程序权限 -> 通道 -> 选择频道.基本.ReadAll和ChannelMessage.Read。全部。添加权限组->群组.阅读.全部.授予相同的权限Microsoft Graph -> 委派权限以及目录.AccessAsUser.All。
- 点击授予管理员同意...

- 然后创建一个秘密来访问该应用程序。前往证书和秘密->新客户秘密,指定密钥名称及其有效期。
复制其中的值价值场地:
价值:your_secret


详细了解如何将 Microsoft Graph API 与 PowerShell 连接。
然后,您可以从 PowerShell 连接到 Microsoft Entra ID (Azure AD) 并获取访问令牌。
$clientId = "your_app_ID"
$tenantName = "yourtenant.onmicrosoft.com"
$clientSecret = "your_secret"
$resource = "https://graph.microsoft.com/"
$Username = "[email protected]"
$Password = "yourpassword"
$ReqTokenBody = @{
Grant_Type = "Password"
client_Id = $clientID
Client_Secret = $clientSecret
Username = $Username
Password = $Password
Scope = "https://graph.microsoft.com/.default"
}
$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody
您可以通过 PowerShell 脚本在 Microsoft 365 中使用基于证书的身份验证。
现在,您可以从 Microsoft 365 租户获取各种数据。
列出租户中的团队:
#Getting all Teams
$header= @{Authorization = "Bearer $($TokenResponse.access_token)"}
$BaseURI = "https://graph.microsoft.com/beta"
$AllMicrosoftTeams = (Invoke-RestMethod -Uri "$($BaseURI)/groups?'$filter=resourceProvisioningOptions/Any(x:x eq 'Team')" -Headers $header -Method Get -ContentType "application/json").value
$AllMicrosoftTeams| FT id, DisplayName,Description
然后按 ID 显示 Teams 组中的频道列表:
# List channels in Team $TeamsID="your_team_id" $TeamsChannels = (Invoke-RestMethod -Uri "$($BaseURI)/teams/$($TeamsID)/channels" -Headers $Header -Method Get -ContentType "application/json").value $TeamsChannels | FT id, DisplayName,Description


您可以使用以下 PowerShell 脚本从 Teams 频道获取消息和回复列表:
$ChannelID="your_chat_id "
$Header =@{Authorization = "Bearer $($Tokenresponse.access_token)"}
$apiUrl = "https://graph.microsoft.com/beta/teams/$TeamsID/channels/$ChannelID/messages"
$Data = Invoke-RestMethod -Uri $apiUrl -Headers $header -Method Get
$Messages = ($Data | Select-Object Value).Value
class messageData
{
[string]$dateTime
[string]$from
[string]$body
[string]$re
messageData()
{
$this.dateTime = ""
$this.from = ""
$this.body = ""
$this.re = ""
}
}
$messageSet = New-Object System.Collections.ArrayList;
foreach ($message in $Messages)
{
$result = New-object messageData
$result.DateTime=Get-Date -Date (($message).createdDateTime) -Format 'yyyy/MM/dd HH:mm'
$result.from = $message.from.user.displayName
$result.body = $message.body.content
$messageSet.Add($result)
#parsing replies
$repliesURI = "https://graph.microsoft.com/beta/teams/" + $TeamsID + "/channels/" + $ChannelID + "/messages/" + $message.ID + "/replies?`$top100"
$repliesResponse = Invoke-RestMethod -Method Get -Uri $repliesURI -Headers $header
foreach ($reply in $repliesResponse.value)
{
$replyData = New-Object messageData
$replyData.dateTime = Get-Date -Date (($reply).createdDateTime) -Format 'yyyy/MM/dd HH:mm'
$replyData.from = $reply.from.user.displayName
$replyData.body= $reply.body.content
$replyData.re="RE"
$messageSet.Add($replyData)
}
}
$messageSet|ConvertTo-Html | Out-File c:psteams_chat_history.html -Encoding utf8
该脚本从指定频道获取对话列表,获取每个对话的回复列表,并生成包含聊天完整内容的 HTML 文件。表中讨论的回复包含关键字段RE。
查看我们的 GitHub 存储库以获取此脚本代码:https://github.com/maxbakhub/winposh/blob/main/teams/export_messages_teams_chat.ps1


