Wednesday, March 19, 2014

Everyday Powershell - Part 17 - Using new-mailboxexportrequest with NON-US dates

Here's a crazy making problem!

The new-mailboxexportrequest commandlet will only accept dates in US formats. Roland Paix has made this post about the issue. The conclusion is switch your culture to EN-US before loading powershell. Sooo frustrating! But hey, something has to be the default datetime and Microsoft is an American company. No point getting emotional, let's just fix it.

Here's a neat trick you can use to set the Culture of the "Current Thread" you are running.

[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
[System.Reflection.Assembly]::LoadWithPartialName("System.Globalization")
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-us")

Add-PSSnapin *exchange*

$start = (get-date).adddays(-28)
$end = (get-date).adddays(-14)

$filter = "(Received -gt '"+$start+"') -and (Received -lt '"+$end+"')"
new-Mailboxexportrequest -mailbox SOMEMAILBOX -ContentFilter $filter -filepath C:\SOME.pst

This should trick new-mailboxexportrequest into believing that your dates are in the "correct" format!

6 comments:

  1. Hi Ben

    I read this article and thought it an ideal solution as changing the server wide culture is something I'd rather avoid. However, I'm having difficulty getting it to work.

    The server is 2012, en-GB with Exchange 2013 v15.0 ‎(Build 847.32) running Mailbox and Client Access Roles. Here is the output:

    PS C:\> [System.Reflection.Assembly]::LoadWithPartialName("System.Threading")

    GAC Version Location
    --- ------- --------
    True v4.0.30319 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Threading\v4.0_4.0.0.0__b03f5f7f11d50a3a\Sys...


    PS C:\> [System.Reflection.Assembly]::LoadWithPartialName("System.Globalization")

    GAC Version Location
    --- ------- --------
    True v4.0.30319 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Globalization\v4.0_4.0.0.0__b03f5f7f11d50a3a...


    PS C:\> [System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCult
    ure("en-us")
    PS C:\> $date = get-date
    PS C:\> $date

    27 June 2014 17:30:59


    PS C:\> $date.ToShortDateString()
    27/06/2014
    PS C:\>

    As you can see the date format is still dd/mm/yyyy. Any idea where I'm going wrong?

    Many Thanks

    John McAlinden

    ReplyDelete
    Replies
    1. John,

      I that's an interesting question. I'll have a poke around and see what I can dig up. First guess is it's just Powershell being "helpful".

      Let me see what I can come up with and I'll make another post when I get the chance.

      Delete
  2. Fabulous script. One thing - I first tried to run it in the Exchange Management Shell and the System Culture did not affect because the cmdlets were already loaded. I needed to run it in Powershell so that it changed the Culture before loading the Exchange Snapins and then it worked like a charm. Thank you for saving me soooo much time.

    ReplyDelete
  3. Any luck by chance for John querry

    ReplyDelete
  4. Thanks for this. It doesn't work as is, but I worked it out - wrap everything in a function otherwise Powershell 'forgets' the Culture you just set.

    Function dostuff {
    [System.Threading.Thread]::CurrentThread.CurrentCulture = "en-US"
    $since=(Get-Date).AddDays(-1)
    echo $since.ToShortDateString()
    Add-PSSnapin *exchange*
    New-MailboxExportRequest -Mailbox "scooper" -ContentFilter "(Received -ge '$since')" -FilePath "\\svrbarb50\l$\PSTExport\scooper3.pst" -ExcludeDumpster
    }

    PS C:\Users\xcooper> dostuff
    3/20/2017

    Name Mailbox Status
    ---- ------- ------
    MailboxExport4 /Users/Cooper, Steve Queued


    PS C:\Users\xcooper> Get-MailboxExportRequest -identity scooper\mailboxexport4

    Name Mailbox Status
    ---- ------- ------
    MailboxExport4 /Users/Cooper, Steve Completed

    ReplyDelete
    Replies
    1. Thank you, it was help me.

      Delete