gpt4 book ai didi

email - Powershell - Outlook 将所有邮件标记为已读然后删除

转载 作者:行者123 更新时间:2023-12-04 17:43:18 25 4
gpt4 key购买 nike

在试图解决这个问题时遇到了一些问题。
由于某种原因,我的脚本无法正常工作。

它应该将收件箱文件夹中的所有邮件标记为已读,然后将其删除。
但是,当脚本运行时,它只会删除 .count $emails 显示的一半...

如何解决这个问题,我做错了什么吗?

$outlook = new-object -comobject outlook.application

#Define folders
$namespace = $outlook.GetNameSpace("MAPI")
$pst = $namespace.Stores
$pstRoot = $pst.GetRootFolder()
$pstFolders = $pstRoot.Folders
#$personal = $pstFolders.Items("ARCHIVE") ##Not working, sadly.
$DefaultFolder = $namespace.GetDefaultFolder(6)
$InboxFolders = $DefaultFolder.Folders
$DeletedItems = $namespace.GetDefaultFolder(3)
$Emails = $DefaultFolder.Items

Foreach ($Email in $Emails) {
#Define folders
$Email.UnRead = $false
$Email.Move($DeletedItems) | out-null
continue
}

最佳答案

我建议使用 MailItem.Delete() method而不是将内容移动到“已删除邮件”文件夹。从 Delete() 方法页面:

The Delete method deletes a single item in a collection. To delete all items in the Items collection of a folder, you must delete each item starting with the last item in the folder. For example, in the items collection of a folder, AllItems, if there are n number of items in the folder, start deleting the item at AllItems.Item(n), decrementing the index each time until you delete AllItems.Item(1).

The Delete method moves the item from the containing folder to the Deleted Items folder. If the containing folder is the Deleted Items folder, the Delete method removes the item permanently.



有了这些知识,我建议用以下内容替换您的 ForEach 循环:
For($i=($emails.count-1);$i -ge 0;$i--){
$($emails)[$i].Unread = $false
$($emails)[$i].delete()
}

我不明白为什么你必须对集合进行子表达式来枚举它,但我从来没有能够在不这样做的情况下指定记录。

关于email - Powershell - Outlook 将所有邮件标记为已读然后删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24829011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com