이 문서에서는 PowerShell을 사용하여 Microsoft Teams 채팅 대화 기록에 액세스하고 내보내는 방법을 살펴보겠습니다.
Teams 채팅은 숨겨진 폴더에 저장됩니다. 대화기록\팀채팅 폴더 공유 사서함, 새 Microsoft 365 그룹을 만들 때 자동으로 생성됩니다(이렇게 하면 Teams 그룹, 웹 사이트, SharePoint Online 라이브러리, Yammer 그룹 등이 즉시 생성됩니다).
그러나 Outlook이나 다른 앱을 사용하면 Teams 채팅 기록이 있는 보호된 폴더에 액세스할 수 없습니다. 당신은 할 수 있습니다 Exchange Online 사서함의 내용을 PST 파일로 내보내기 사용하여 콘텐츠 검색에서 보안 및 규정 준수 센터를 선택한 다음 Outlook에서 PST 파일을 연결합니다. 하지만 너무 편리하지는 않습니다. PowerShell을 사용하여 Teams 채팅 메시지 목록을 얻는 것이 훨씬 쉽습니다.
Microsoft 365 테넌트에 연결하기 위해 Microsoft Graph API를 사용합니다.
- 새로 만들기 appTeamsView Azure Portal의 앱(Azure AD -> 앱 등록 -> 신규 등록)
- 다음 값을 복사합니다.
애플리케이션(클라이언트) ID:your_app_ID
디렉터리(테넌트) ID:your_tenant_ID
- API 권한으로 이동하여 Microsoft Graph -> 응용 프로그램 권한 -> 채널 -> 선택을 클릭합니다. 채널. 기초적인. 모두 읽기 그리고 채널메시지. 읽다.모두. 권한 그룹 추가 -> 그룹. 읽다. 모두. 동일한 권한을 부여하십시오. Microsoft Graph -> 위임된 권한 및 예배 규칙서. AccessAsUser. 모두.
- 딸깍 하는 소리 다음에 대한 관리자 동의 부여…
- 그런 다음 앱에 액세스하기 위한 비밀을 만듭니다. 이동 인증서 및 비밀 -> 새로운 클라이언트 비밀에서 키 이름과 유효 기간을 지정합니다.
다음에서 값을 복사합니다. 값 필드:
값:your_secret
그런 다음 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.
이제 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:\ps\teams_chat_history.html -Encoding utf8.
이 스크립트는 지정된 채널에서 대화 목록을 가져오고, 각 대화에 대한 응답 목록을 가져오고, 채팅의 전체 내용이 포함된 HTML 파일을 생성합니다. 테이블의 토론에 대한 답변에는 키 필드가 포함되어 있습니다. RE
.
이 스크립트 코드는 GitHub 저장소를 확인하세요. https://github.com/maxbakhub/winposh/blob/main/teams/export_messages_teams_chat.ps1