Skip to content

Instantly share code, notes, and snippets.

@mbourgon
Created February 5, 2024 17:29
Show Gist options
  • Save mbourgon/61058c09f3b04bfb81d5f9415a1ca85e to your computer and use it in GitHub Desktop.
Save mbourgon/61058c09f3b04bfb81d5f9415a1ca85e to your computer and use it in GitHub Desktop.
Teams - Full Export of your chats and messages
#Who: Michael Bourgon 2024
#What: save out all your messages/chats/meetings from Teams to HTML pages for later searching
#future fixes: change the filedate to be the time of the last message. Doubt it matters, though, since you'll be scanning this and see that it's archived.
# Requires the microsoft graph powershell module 1.27 or better.
$RequiredScopes = @("Chat.ReadBasic", "Chat.ReadWrite")
Connect-MgGraph -Scopes $RequiredScopes
$startdate = (get-date).AddDays(-3000) #because for oneOnOnes, I want a lot, and it searches for changes, not meetings with active messages - doesn't appear to be a way
$meetingchanged = (get-date).AddDays(-210) #lookback for meeting changes, only include meetings with changes more recent than this
# (note that "changes" means new members added, change of name, etc)
$myrecentchats = @()
write-host "getting the recent meeting chats"
#how to test out get-mgchat and get the full error
#try {get-mgchat -all -PageSize 50 -filter "lastUpdatedDateTime gt '$oldtime' and ChatType eq 'meeting'" -ErrorAction Stop -Debug } catch { Write-Host $_.Exception | fl * }
$oldtime = $($meetingchanged.ToString("s") + "Z")
#setting up the table header & footer
$htmlheader = @"
<!DOCTYPE html><html><head> <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid black; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } </style></head><body> <table> <thead> <tr> <th>Last Modified Date</th> <th>From</th> <th>Body</th> </tr> </thead> <tbody>
"@
$htmlfooter = @"
</tbody> </table></body></html>
"@
#this gets all your chats within the range.
#somewhere between 1.23 and 1.27 they made it so using quotes around $oldtime stopped working (...gt '$oldtime' and...). No quotes now! And you have to COMPLETELY uninstall,#
# then use -requiredversion 1.27.00
$myrecentchats = get-mgchat -all -PageSize 50 -filter "lastUpdatedDateTime gt $oldtime and ChatType eq 'meeting'"
write-host "getting all oneonones"
$myrecentchats += get-mgchat -all -PageSize 50 -filter "ChatType eq 'oneOnOne'"
write-host "getting all group chats"
$myrecentchats += get-mgchat -all -PageSize 50 -filter "ChatType eq 'group'"
#now we walk through each chat, grab its messages, and create a web page for them.
foreach ($chat in $myrecentchats) {
#set some details about the chat itself for the later query. Maybe those are only used by "Teams", not "Chat"?
#$chatinfo = get-mgchat -ChatId $chat.id
$chatid = $chat.id.replace(":","^")
$chatname = $chat.Topic #tried doing something smart, doesn't work if there's no topic. #.replace("\","-").replace("/","-").replace(":","---").replace("[","").replace("]","")
$members = $chat.Members
$chattype = $chat.chattype
#erase info about it so we don't dupe or put in the wrong file
$recentchatmessages = @()
$recentchatmessages = get-mgchatmessage -ChatId $chat.id -Filter "lastModifiedDateTime gt $($startdate.ToString("s") + "Z")" -pagesize 50 -all
#and now use select expression to set the table.
# $recentchatmessages | select -first 1 @{Label='ChatName';Expression={($chatname)}}, @{Label='members';Expression={($members)}}, @{Label='ChatType';Expression={($chattype)}}, @{Label='ChatID';Expression={($chatid)}}|convertto-html |out-file "c:\temp\Teams_Export_2024\$($chatid).html" -Append
# #and finally get
# $recentchatmessages | sort -property LastModifiedDateTime| select @{Label='LastModified';Expression={"<td>"+($_.LastModifiedDateTime.tolocaltime())+"</td>"}},
# @{Label='From';Expression={"<td>" + ($_.from.user.displayname)+"</td>"}}, @{Label='Body';Expression={ "<td>"+($_.Body.content -split '\n')+"</td>"}} |convertto-html |out-file c:\temp\$($chatid).html -Append
$myhtmlb = $recentchatmessages | sort -property LastModifiedDateTime| select @{Label='tablerow';Expression={"<tr>"+ "<td>"+($_.LastModifiedDateTime.tolocaltime())+"</td>"+ "<td>" + ($_.from.user.displayname)+"</td>" + "<td>"+($_.Body.content -split '\n')+"</td>" + "</tr>" }}
#now write that to a file.
#if the path doesn't exist, create it
If (!(Test-Path "c:\temp\Teams_Export_2024\$chattype\"))
{
New-Item -ItemType Directory "c:\temp\Teams_Export_2024\$chattype"
}
write-host $chatid
#this needs to be three parts. Topic has to exst so can't add it there, but I can fix the paths here
$simplefilename = ($chatid).replace("\","-").replace("/","-");
#remove ALL the crap characters https://stackoverflow.com/questions/38044236/test-path-illegal-characters-in-path
$simplefilename.Split([IO.Path]::GetInvalidFileNameChars()) -join ''
$chatname + $htmlheader +$myhtmlb.tablerow + $htmlfooter |Out-File -FilePath "c:\temp\Teams_Export_2024\$chattype\$simplefilename.html" -Encoding ascii
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment