移动Outlook邮件Powershell [英] Move Outlook Messages Powershell
问题描述
我正在尝试编写一个脚本,该脚本从10天以上的文件夹中的邮件项目中下载附件,将其保存到我的硬盘驱动器中,然后将邮件项目移至已删除邮件"文件夹中.到目前为止,附件下载工作正常,但是当我添加代码以移动电子邮件时,它仅对文件夹中一半的电子邮件有效.代码如下所示.任何帮助表示赞赏.
I'm trying to write a script that downloads the attachments from mail items in a folder that are older than 10 days, save them to my harddrive and then move the mail items to the deleted items folder. So far the attachment download works fine, but when I add in the code to move the emails, it only works on half the emails in my folder. Code shown below. Any help appreciated.
$Folder.Items | foreach{
$Received = New-Object DateTime
$Received = $_.ReceivedTime
$howOld = New-Object int
$howOld = $Today.subtract($Received)
$howOld = $howOld | select-object -ExpandProperty Days
$date = $_.ReceivedTime.ToString("MMddyyyy")
$howOld
if ($howOld -gt 10){
#Save Attachment To Folder
$_.attachments | foreach{
$fileName = $_.filename
$split = $fileName.Split('.')
$fileName = $split[0]
$fileExtension = $split[1]
$fileName = $fileName + '_' + $date + '.' + $fileExtension
$_.saveasfile((Join-Path $filepath $fileName))
}
#Move to Deleted Folder
$result = $_.Move($deletedFolder)
}
}
推荐答案
能够通过更改循环结构来解决此问题.而不是使用foreach,我改为使用迭代计数器.
Was able to fix the problem by changing the looping structure. Instead of using a foreach i changed to an iterative counter.
$emails = $Folder.Items
For ($i=($emails.count-1);$i -ge 0;$i--)
这样可以解决问题.
这篇关于移动Outlook邮件Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!