Hi @Roger Roger
I just used your script locally to export a .csv file of the message set. Even though the subject is empty, it still outputs fine.
So, I suggest you output the $messages variable in a PowerShell context and see if it returns your message set fine. If the issue occurs when outputting the $messages variable in a PowerShell context, then you may need to do some judgment on the message set returned. If it outputs fine in a PowerShell context, then it may be that some data was lost when converting to a .csv file.
$TeamId = "Teams-ID"
$ChannelId = "Channel-ID"
$accessToken = "tokengenerated"
$headers = @{
"Authorization" = "Bearer $accessToken"
}
# Retrieve messages
$messages = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/teams/$TeamId/channels/$ChannelId/messages" -Headers $headers
# Create an empty array to store messages
$messageList = @()
# Iterate through each message
foreach ($message in $messages.value) {
$subject = $message.subject
$body = $message.body.content
$id = $message.id
$from = $message.from.user.displayName
$createdDateTime = $message.createdDateTime
# If the subject is empty but there is a body, or there is a subject, but the body is empty, or both exist
if ($subject -or $body) {
$messageList += [PSCustomObject]@{
Id = $id
From = $from
CreatedDateTime = $createdDateTime
Subject = $subject
Body = $body
}
}
}
# Export to CSV file
$messageList | Export-Csv -Path "C:\output.csv" -NoTypeInformation
Hope this helps.
If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.