Friday, December 5, 2014

Getting New-MailboxExportRequest work with date variables in ContentFilter


The following script exported the full mailbox, ignoring the date range in ContentFilter. I couldn't figure it out. Found a lot of people with the same problem. 

$StartDate = (Get-Date).AddDays(-1).Date
$EndDate = (Get-Date).Date
$FilePath = "\\server\export.pst"
New-MailboxExportRequest -Mailbox peter -Name TestExport -FilePath $FilePath -ContentFilter {(Received -gt $StartDate) -and (Received -lt $EndDate)}
I finally found a solution here: http://occasionalutility.blogspot.com.au/2014/03/everyday-powershell-part-17-using-new.html Turns out it will only accept dates in US formats, and since I'm in Sweden it just failed.

The working script looks like this:

[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
[System.Reflection.Assembly]::LoadWithPartialName("System.Globalization")
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-us") 
$StartDate = (Get-Date).AddDays(-1).Date
$EndDate = (Get-Date).Date
$FilePath = "\\server\petero-"+(Get-Date).AddDays(-1).ToString("yyyy-MM-dd")+".pst"
$filter = "(Received -gt '"+$StartDate+"') -and (Received -lt '"+$EndDate+"')"
New-MailboxExportRequest -Name TestExport -Mailbox peter -ContentFilter $filter -FilePath $FilePath