![如何使用 PowerShell 将 Exchange 项目移动到 PST?](https://linux22.com/image/612449/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20PowerShell%20%E5%B0%86%20Exchange%20%E9%A1%B9%E7%9B%AE%E7%A7%BB%E5%8A%A8%E5%88%B0%20PST%EF%BC%9F.png)
我们有一个邮箱,用于接收需要永久保存的状态电子邮件,并且这些电子邮件可能包含很大的附件。我需要能够定期(每天或每周)将这些邮件移动到 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
问题是我必须每次在四个地方手动更新日期参数,我担心这不太交易性(如果在这种情况下这甚至有意义的话)。
我正在尝试创建一个小型 PS 脚本来按月遍历日期并构建一组命令来循环播放和导出这些信息,但想知道是否有更简单的方法来“移动到 PST”,或者我是否需要尝试动态构建上述语句。
这是我目前拥有的更完整的脚本,它应该导出本月初之前 10 天的所有项目(以加快测试速度):
$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 似乎包含所有电子邮件!
答案1
使用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
每月运行一次即可存档最近几个月的电子邮件
答案2
如果您愿意将其放入计划任务中,那么这应该可以得到您想要的内容:
$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 的日期处理功能。首先将$fromtime
和$totime
变量归零到午夜,然后设置为一天的距离。然后我们在构建 Exchange cmdlet 所需的查询时使用这些变量。