如何使用PowerShell将Exchange项目移动到PST?

我们有一个邮箱,可以收到我们需要永久保留的状态邮件,并且包含潜在的大附件。 我需要能够定期(每天或每周)将这些消息移动到PST,所以想自动执行此操作。

目前我正在使用MailboxExportRequest将它们导入PST:

 New-MailboxExportRequest -ContentFilter {(Received -ge '01/01/2013') -and (Received -lt '01/02/2013')} -Mailbox "MonitorMailbox" -FilePath "\\Server\backup\EmailLog\MonitorMailbox 2013-01.pst" 

我跟着一个SearchMailbox -DeleteContent

 Get-Mailbox -Identity "MonitorMailbox" | Search-Mailbox -SearchQuery "Received:01/01/2013..01/02/2013" -DeleteContent 

问题是,我不得不手动更新四个地方的date参数,我担心它不是非常事务性的 (如果这在上下文中是有意义的)。

我正在尝试创build一个PS脚本来遍历每个月份的date,并build立一组命令来旋转和导出这些信息,但想知道是否有一个更简单的方法来“移动到PST”,或者如果我需要尝试dynamic地构build上述语句。


到目前为止,这是一个更完整的脚本,它应该在本月初之前的10天内导出所有项目(以加速testing):

 $mailbox = "Cylindric" $endDate = Get-Date -Day 1 "00:00:00" $startDate = $endDate.AddDays(-10) $month = "{0:D2}" -f [int]$startDate.Month $year = "{0:D4}" -f [int]$startDate.Year Write-Host -NoNewline "Exporting items between $startDate and $endDate..." New-MailboxExportRequest -Name "EmailLog$year$month" -ContentFilter {(Received -ge $startDate) -and (Received -lt $endDate)} -Mailbox $mailbox -FilePath "\\ReadyNAS\backup\Mailboxes\EmailLog\EmailLog${year}-${month}.pst" Write-Host "Done." Write-Host -NoNewline "Waiting for export to complete..." While(!(Get-MailboxExportRequest -Mailbox $mailbox -Name "EmailLog$year$month" -Status Completed)) { #Sleep for a few minutes Write-Host -NoNewline "." Start-Sleep -s 60 } Write-Host "Done." Write-Host -NoNewline "Removing Export Request..." Get-MailboxExportRequest -Mailbox $mailbox -Status Completed -Name "EmailLog$year$month" | Remove-MailboxExportRequest Write-Host "Done." 

而输出:

 Exporting items between 05/22/2013 00:00:00 and 06/01/2013 00:00:00... Name Mailbox Status ---- ------- ------ EmailLog201305 Cylindric Queued Done. Waiting for export to complete..........Done. 

虽然这似乎比这10天的出口量要多得多。 PST似乎包含所有电子邮件!

使用Get-Date cmdlet!

下面的代码将检查它是哪一个月,减去1并将所有邮件导出到您的.pst文件

 $endDate = Get-Date -Day 1 "00:00:00" $startDate = $endDate.AddMonths(-1) $month = "{0:D2}" -f [int]$startDate.Month # Convert dates to short date strings $endDate = $endDate.ToShortDateString() $startDate = $startDate.ToShortDateString() New-MailboxExportRequest -ContentFilter {(Received -ge $startDate) -and (Received -lt $endDate)} -Mailbox "MonitorMailbox" -FilePath "\\Server\backup\EmailLog\MonitorMailbox 2013-${month}.pst" while(!(Get-MailboxExportRequest -Mailbox "MonitorMailbox" -Status Completed)) { #Sleep for a few minutes Start-Sleep -s 300 } Get-MailboxExportRequest -Mailbox "MonitorMailbox" -Status Completed | Remove-MailboxExportRequest Get-Mailbox -Identity "MonitorMailbox" | Search-Mailbox -SearchQuery "Received:'${startDate}'..'${endDate}'" -DeleteContent 

简单运行一个月,以获得最后一个月的电子邮件存档

如果你愿意把它放到一个计划任务中,这应该得到你正在寻找的东西:

 $now=get-datetime $fromtime=$now.addHours(-$now.hour).addMinutes(-$now.minute).addSeconds(-$now.second).addDay(-2) $totime=$now.addHours(-$now.hour).addMinutes(-$now.minute).addSeconds(-$now.second).addDay(-1) New-MailboxExportRequest -ContentFilter {(Received -ge $fromtime) -and (Received -lt $totime)} -Mailbox "MonitorMailbox" -FilePath "\\Server\backup\EmailLog\MonitorMailbox "+$totime.year+"-"+$totime.month+"-"+$totime.day"+".pst" $mailQuery="Received:"+fromtime.toString("d")+".."+$totime.toString("d") Get-Mailbox -Identity "MonitorMailbox" | Search-Mailbox -SearchQuery $mailQuery -DeleteContent 

它所做的就是利用Powershell的date处理function。 $fromtime$totimevariables首先归零到午夜,然后设置为一天的距离。 然后,我们使用这些variables来构buildExchange cmdlet所需的查询。