PowerShell-下载之前如何过滤附件
我正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的代码将导致脚本下载和处理所有收件箱消息。这就像SQL中的选择语句,而没有某个子句 - 虽然明智地表现出色。
使用
items.find/findNext
或items.restrict
(请参阅 https://learn.microsoft.com/en-us/office/vba/api/opi/outlook.items.find ) - 让服务器/消息商店做这项工作。对于您的第一个查询,用于第二查询,即使扩展MAPI(仅C ++或Delphi)即使在附件名称上进行搜索,也不会让您在
PR_MESSAGE_ATTACHEMENT上公开该功能
并指定pr_attach_long_filename
作为搜索属性)。您当然只能在查询匹配项上使用第一个查询和循环,对于每个条目循环循环附件
mailitem.attachments.attachments collection collection-远非理想,但是仍然总比没有限制好。如果使用 enderemption (我是它的作者 - 它是一个扩展的Mapi Wrapper从任何语言中使用)是一个选项,它允许在查询中使用
附件
。像以下内容(在我的头顶上,VBA):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
orItems.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, useFor 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
onPR_MESSAGE_ATTACHMENTS
and specifyPR_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 eachAttachment
object in theMailItem.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):