问题
I have created a very simple powershell script to collect some basic information about all the emails on a shared mailbox folder.
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$Inbox = $namespace.pickfolder()
$Inbox.items | Select-Object -Property Categories, Attachments, Subject, ReceivedTime
This is working as expected, printing a table which can be exported as CSV, but the Attachments
property just print System.__ComObject
. So, I need to replace the "Attachments" column with a list of the attachments names ($_.Attachments::DisplayName
)
I tried to iterate the results to fix this, with something like:
$Inbox.items | Select-Object -Property Categories, Attachments,
Subject, ReceivedTime | ForEach-Object -Process {$_.Attachments::DisplayName}
But this prints only the attachment information and not the rest of the data. Also, adding all the fields between {} cause powershell to print one field per line, so it can't be exported as CSV.
Can you help me to replace the Attachments column properly?
回答1:
Change the last line:
$Inbox.items |Select-Object -Property Categories,Subject,
ReceivedTime,@{name="Attachments";expression={$_.Attachments|%{$_.DisplayName}}}
you can then pipe it to | ft -wrap
to "expand" the Attachments column.
来源:https://stackoverflow.com/questions/21940851/listing-all-emails-on-a-shared-mailbox-with-attachments-name