Outlook Items.Restrict不返回所有邮件

时间:2011-10-11 15:59:23

标签: powershell outlook office-interop outlook-2007 mapi

我有一个小的PowerShell脚本,它使用Outlook互操作将某些邮件从收件箱移动到其他文件夹。基本移动操作使用以下代码完成:

[Reflection.Assembly]::LoadWithPartialname("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.getDefaultFolder($olFolders::olFolderInbox)
$filter = "[SenderName] = 'Dummy Sender'"
$messages = $inbox.items.Restrict($filter)
$messages | % { 
    Write-Host "`t$($_.Subject)" 
    [void]$_.Move($destination) | Out-Null
}

我注意到的问题是items.Restrict没有返回所有匹配的消息。每次运行脚本时,我都会得到3到20条消息。

之前有没有人观察过这种行为?有什么明显的东西我不见了吗?

1 个答案:

答案 0 :(得分:4)

当您还在循环中时,您正在修改集合。 将条目ID保存到静态数组/列表中,然后一次重新打开一条消息,或者使用从倒数到1的循环。