The wider context of this is that I am trying to write some powershell code to extract a listing of the contents of my Office 365 mailbox in CSV form including the size of each item to help me to do some subsequent analysis of where the storage in my mailbox is being consumed. I can see how this functionality would also lead onto other useful tools.
I discovered this sample/POC code by Aleksandr Reznik EXchangeOnline2CSV this got me a lot of the way there. However I've been unable to successfully extend it to extract the mail size (in bytes).
I found this similar question using the graph.microsoft.com API. And from that I've inferred (perhaps wrongly) that the powershell module i'm using (Microsoft.Graph.Mail) is obtaining it's data from the same source and that message size is a "single value extended property" of a mail object.
The Powershell MS Graph Docs are a bit sparse, however they have a cmdlet that seems to be the right candidate for interfacing with this data Get-MgUserMessageSingleValueExtendedProperty
I've also found similar code in the Exch-Rest source that looks like it's doing what I need (in a way I admit I don't quite understand) but that has given me hope that what I'm trying to do is actually possible. This code includes a call to extract the "MessageSize" using the same Propety ID from above "0x0E08" Get-EXRKnownProps and Get-EXRTaggedProperty doesn't seem to be doing anything too peculiar to extract it (although I don't quite follow all of it).
My attempts at trying to get Get-MgUserMessageSingleValueExtendedProperty
to return anything at all have been pretty futile however.
I feel like this is either a "it hasn't been implemented yet" or a "you're missing a semi-colon" type issue but at this point my google-fu has failed me and I can find no examples of what I'm trying to do so would appreciate any help.
Powershell - 5.1.19041.1682 Microsoft.Graph - 1.16.0
Here's some butchered code that I have been using to make the connection and test the Get-MgUserMessageSingleValueExtendedProperty
cmdlet, including in the inline comments the errors it has been returning.
(UPN changed obviously, however this works for me and returns sensible values for Subject for example)
`
$UPN ="[email protected]"
Connect-MgGraph -Scopes "Mail.Read"
$folders = Get-MgUserMailFolder -UserId $UPN -All
write-host "Current folders:"
$folders.DisplayName
$folder = $folders | Where-Object { $_.DisplayName -eq "Inbox" }
$mails = Get-MgUserMailFolderMessage -All -UserId $UPN -MailFolderId $folder.Id -Top 1
Write-Host "No. Of Emails: $($mails.count)"
foreach($currEmail in $mails){
Write-Host "Subject: $($currEmail.Subject)"
# PidTagMessageSize = 0x0E08
# Can't get the right form for the following....
$mailSizeInBytes = Get-MgUserMessageSingleValueExtendedProperty -InputObject $currEmail -Property "0x0E08"
# Errors as Get-MgUserMessageSingleValueExtendedProperty : The pipeline has been stopped.
$mailSizeInBytes = Get-MgUserMessageSingleValueExtendedProperty -MessageId $currEmail.Id -UserId $UPN -Property "0x0E08"
# Errors as Get-MgUserMessageSingleValueExtendedProperty : Parsing OData Select and Expand failed: An identifier was expected at position 0.
$mailSizeInBytes = Get-MgUserMessageSingleValueExtendedProperty -MessageId $currEmail.Id -UserId $UPN -Property "LONG 0x0E08"
# Errors as Get-MgUserMessageSingleValueExtendedProperty : Parsing OData Select and Expand failed: Term 'LONG 0x0E08' is not valid in a $select or $expand expression.
Write-Host "Mail Size: $($mailSizeInBytes)"
}
`
CodePudding user response:
You can use the -expandproperty in Get-MgUserMailFolderMessage which will equate to an $expand in the Graph request eg
$mails = Get-MgUserMailFolderMessage -All -UserId user@domain -MailFolderId Inbox -Top 1 -ExpandProperty "singleValueExtendedProperties(`$filter=id eq 'long 0x0E08')"
then
$mails.SingleValueExtendedProperties[0]