如何使用 PowerShell 導出 MS Teams 聊天歷史記錄

Jacki

在本文中,我們將了解如何使用 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 聊天發送消息。

  1. 創建一個新的應用程序團隊視圖Azure 門戶中的應用程序(Azure AD -> 應用程序註冊 -> 新註冊);
  2. 複製以下值:
    應用程序(客戶端)ID:your_app_ID
    目錄(租戶)ID:your_tenant_ID
  3. 轉到 API 權限,單擊 Microsoft Graph -> 應用程序權限 -> 通道 -> 選擇頻道.基本.ReadAllChannelMessage.Read全部。添加權限組->群組.閱讀.全部.授予相同的權限Microsoft Graph -> 委派權限以及目錄.AccessAsUser.All。
  4. 點擊授予管理員同意...
  5. 然後創建一個秘密來訪問該應用程序。前往證書和秘密->新客戶秘密,指定密鑰名稱及其有效期。
    複製其中的值價值場地:
    價值: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 組中的頻道列表:

建議閱讀:如何刪除 Microsoft 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