当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 导出 MS Teams 聊天历史记录

作者:精品下载站 日期:2024-12-14 06:36:09 浏览:14 分类:玩电脑

如何使用 PowerShell 导出 MS Teams 聊天历史记录


在本文中,我们将了解如何使用 PowerShell 访问和导出 Microsoft Teams 聊天对话的历史记录。

Teams 聊天存储在共享邮箱中隐藏的对话历史记录\Team Chat 文件夹中,该文件夹是在您创建新的 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 聊天发送消息。

  1. 在 Azure 门户中创建一个新的 appTeamsView 应用程序(Azure AD -> 应用程序注册 -> 新注册);

  2. 复制以下值:
    应用程序(客户端)ID:

    your_app_ID

    目录(租户)ID:

    your_tenant_ID
  3. 转到 API 权限,单击 Microsoft Graph -> 应用程序权限 -> 频道 -> 选择 Channel.Basic.ReadAllChannelMessage.Read全部 。添加权限组 -> Group.Read.All。在 Microsoft Graph -> 委派权限以及 Directory.AccessAsUser.All 中授予相同的权限。

  4. 单击授予管理员同意...

    [玩转系统] 如何使用 PowerShell 导出 MS Teams 聊天历史记录

  5. 然后创建一个秘密来访问该应用程序。转到证书和机密 -> 新客户端机密,指定密钥名称及其有效期。
    复制值> 字段:
    值:

    your_secret

[玩转系统] 如何使用 PowerShell 导出 MS Teams 聊天历史记录

详细了解如何将 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 "
$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 导出 MS Teams 聊天历史记录

您可以使用以下 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:\ps\teams_chat_history.html -Encoding utf8

该脚本从指定频道获取对话列表,获取每个对话的回复列表,并生成包含聊天完整内容的 HTML 文件。表中讨论的回复包含关键字段

RE

查看我们的 GitHub 存储库以获取此脚本代码:https://github.com/maxbakhub/winposh/blob/main/teams/export_messages_teams_chat.ps1

[玩转系统] 如何使用 PowerShell 导出 MS Teams 聊天历史记录

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯