PowerShell-下载之前如何过滤附件

发布于 2025-01-23 01:21:23 字数 460 浏览 0 评论 0原文

我正在使用PowerShell脚本从具有多个附件的电子邮件下载附件。

如果我使用以下语句,它将下载所有附件。

# Find Unread mail messages
$UnreadMessages = $Inbox.Items | where-object {$_.Unread -and $_.SenderEmailAddress -imatch "usa"}

我只想使用以下语句下载特定的附件,但没有任何东西。

# Find Unread mail messages
$UnreadMessages = $Inbox.Items | where-object {$_.Unread -and $_.SenderEmailAddress -imatch "usa" -and $_.Attachments -imatch "Backlog"}

请帮助我更正此声明

I am using a PowerShell script to download attachment from an email which has multiple attachment.

If I use below statement it will download all the attachments.

# Find Unread mail messages
$UnreadMessages = $Inbox.Items | where-object {$_.Unread -and $_.SenderEmailAddress -imatch "usa"}

I want to download only a specific attachment using below statement but it gives nothing.

# Find Unread mail messages
$UnreadMessages = $Inbox.Items | where-object {$_.Unread -and $_.SenderEmailAddress -imatch "usa" -and $_.Attachments -imatch "Backlog"}

Please help me correct this statement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

維他命╮ 2025-01-30 01:21:23

首先,您的代码将导致脚本下载和处理所有收件箱消息。这就像SQL中的选择语句,而没有某个子句 - 虽然明智地表现出色。

使用items.find/findNextitems.restrict(请参阅 https://learn.microsoft.com/en-us/office/vba/api/opi/outlook.items.find ) - 让服务器/消息商店做这项工作。对于您的第一个查询,用于

@SQL=("urn:schemas:httpmail:read" = 0) AND ("http://schemas.microsoft.com/mapi/proptag/0x0065001F" like '%usa%')

第二查询,即使扩展MAPI(仅C ++或Delphi)即使在附件名称上进行搜索,也不会让您在PR_MESSAGE_ATTACHEMENT上公开该功能并指定pr_attach_long_filename作为搜索属性)。您当然只能在查询匹配项上使用第一个查询和循环,对于每个条目循环循环附件 mailitem.attachments.attachments collection collection-远非理想,但是仍然总比没有限制好。
如果使用 enderemption (我是它的作者 - 它是一个扩展的Mapi Wrapper从任何语言中使用)是一个选项,它允许在查询中使用附件。像以下内容(在我的头顶上,VBA):

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Folder = Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
set restrItems = Folder.Items.Restrict(" (UnRead = 'true') AND (""http://schemas.microsoft.com/mapi/proptag/0x0065001F"" like '%usa%') AND (Attachments LIKE '%Backlog%')")
for each item in restrItems
  Debug.Print item.Subject
next

Firstly, your code will cause all Inbox messages to be downloaded and processed by your script. This is like a SELECT statement in SQL without a WHERE clause - as bad as it gets performance wise.

Use Items.Find/FindNext or Items.Restrict (see https://learn.microsoft.com/en-us/office/vba/api/outlook.items.find) - let the server/message store do the work. For your first query, use

@SQL=("urn:schemas:httpmail:read" = 0) AND ("http://schemas.microsoft.com/mapi/proptag/0x0065001F" like '%usa%')

For the second query, OOM won't let you search on the attachment name even though Extended MAPI (C++ or Delphi only) exposes that functionality (create RES_SUBRESTRICTION on PR_MESSAGE_ATTACHMENTS and specify PR_ATTACH_LONG_FILENAME as the search property). You can of course use only your first query and loop over the query matches, for each entry looping through each Attachment object in the MailItem.Attachments collection - far from ideal, but still better than no restriction at all.
If using Redemption (I am its author - it is an Extended MAPI wrapper and can be used from any language) is an option, it allows to use Attachments in queries. Something like the following (off the top of my head, VBA):

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Folder = Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
set restrItems = Folder.Items.Restrict(" (UnRead = 'true') AND (""http://schemas.microsoft.com/mapi/proptag/0x0065001F"" like '%usa%') AND (Attachments LIKE '%Backlog%')")
for each item in restrItems
  Debug.Print item.Subject
next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文