Listing all emails on a shared mailbox with attachments name

回眸只為那壹抹淺笑 提交于 2021-02-10 06:44:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!