我有一个用户,在Outlook的收件箱的子文件夹中有成千上万的电子邮件。我们希望将2016年以来收到的所有电子邮件存档到O365的在线存档邮箱中。
我最终会收到错误消息,我必须在代码中丢失一些东西以进行自我清理:“您的服务器管理员限制了您可以同时打开的项目数量。请尝试关闭已打开的邮件,或者从中删除附件和图像您未撰写的邮件。”
下面的代码。我从网上找到的其他各个项目中总结了这一点,所以它可能不是最佳选择,我愿意接受建议。
$filter = "[ReceivedTime] >= '#1/1/2016#' and [ReceivedTime] <= '#12/31/2016#'"
$Folder = "Filing Cabinet"
$yearFolder = "2016"
$starttime = get-date
$srcMailbox = "user@domain.com"
$trgMailbox = "Online Archive - user@domain.com"
$counter=0
$Outlook = New-Object -ComObject "Outlook.Application"
$Mapi = $Outlook.GetNamespace("mapi");
$Emails = ($Mapi.Folders("$srcMailbox")).Folders($Folder)
$ToMove = $EMails.Items.Restrict($filter)
$TargetFolder = ($Mapi.Folders("$trgMailbox")).Folders($yearFolder).Folders($Folder)
foreach($item in $ToMove){
$item.Move($TargetFolder) | out-null
write-host "$($item.receivedtime) - $($item.subject)"
$counter++
}
write-host "$($counter) emails processed."
write-host "Processing started at $($starttime); ended at $(get-date)."