stars
(c)

July 3, 2016

Exchange 2013 Journal Mailbox Archive-to-PST PowerShell Script Automation with Reporting

Recent Exchange versions have built-in support of journaling for recording all inbound and outbound email messages for backup or compliance reasons. Overtime, the journal mailbox grows so large and needs to be trimmed or pruned.

This article documents a PowerShell maintenance script I have written for a customer to report and automate the monthly archive-to-PST process of the Exchange 2013 journaling mailbox.

Archiving Concept

This script uses the PowerShell cmdlet New-MailboxExportRequest -Mailbox to export Exchange 2013 Journaling Mailbox of previous month (e.g. 2016-01-01 to 2016-01-31) as a standard PST file (e.g. archive 2016_01_31.pst) to specified locations (up to two locations) and then uses Search-Mailbox -DeleteContent to delete email messages within the date range if successful. It is designed to be run at the beginning of each month (e.g. 2/Feb/16) using Windows Task Scheduler.

Email Alerting, Reporting and Logging

A log file is created under a specified directory during each execution. An email is sent to specified email addresses with the log file when done. The email message subject indicates COMPLETE/FAILURE.

In addition, a list of email messages returned from search and deleted by Search-Mailbox cmdlet will be available as zip(csv) attachments in another specified mailbox and folder as mentioned in the message body of above email.

Safety Measure to Prevent Accidental Email Deletion

In case mail archiving has failed, script will send an alert mail and exit so that no mail deletion will occur. In technical terms, when status of Get-MailboxExportRequest is not "Completed", script keeps on waiting (looping) until it is complete. If the loop is broken or script receives any status other than "Completed", execution will be terminated and failure will be reported by email.

Assumptions and Requirements

  • PowerShell 3.0 or above
  • Execute this script on Exchange 2013 server where:
    • System Locale is English (United States)
    • Short date format (for en-US) is MM/dd/yyyy
  • Sufficient disk space for storing PST files in the specified location(s)

Note: This script was designed and tested for email messages of previous month, and has not been tested in other configurations but could still work with some tweaking. (In fact, it does not only work for journal mailbox.)

Script Usage and Instructions

  1. Edit variable parameters as required (See sections 1-4 in the script below.)
  2. Save the script in a local folder e.g. C:\scripts\ws_email_archiving_script.ps1
  3. To export email of last month, the script should be scheduled to run any day within the current month.

    For example, to export Jan 2016 email, running the script any day within Feb 2016 would do; the beginning of month is recommended (e.g. 2/Feb/16)

Setting up Windows Task Scheduler

Program/script:

  • C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments:

  • -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V15\Bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\Bin\Exchange.ps1'; &'C:\scripts\ws_email_archiving_script.ps1'"

Start in:

  • C:\Scripts

Alternatively, start the .ps1 script using a .bat script:

  • "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V15\Bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\Bin\Exchange.ps1'; &'C:\scripts\ws_email_archiving_script.ps1'"

PowerShell Script

Download/View on GitHub

https://github.com/wandersick/ws-email-archiving

References

  1. How can I move Exchange items to a PST using PowerShell?
    http://serverfault.com/questions/511610/how-can-i-move-exchange-items-to-a-pst-using-powershell

  2. MailboxExportRequest ContentFilter is “Received -ne $null” when querying by date
    http://serverfault.com/questions/591798/mailboxexportrequest-contentfilter-is-received-ne-null-when-querying-by-date

  3. Cannot get Exchange PowerShell script to run under Scheduled Tasks
    http://serverfault.com/questions/576302/cannot-get-exchange-powershell-script-to-run-under-scheduled-tasks

Release History

Ver Date Update Code Versioning
1.2 20171022 - Turn journal mailbox name into a variable
- Further ensure New/Get-MailboxExportRequest job name uniqueness by naming it with current date time (in yyyyMMdd_hhmmsstt)
- Remove unused variables, scriptDir, startDateShort and endDateShort
- Improve script comments
View Code
1.1 20160712 First public release View Code
1.0
20160314 First internal release N/A
Share/Bookmark

2 comments :

  1. Great Idea! thanks for that, How i can change this for take daily backups?

    ReplyDelete
    Replies
    1. Hi Enes S, thanks for your interest in the script. To clarify your question, do you mean that you want to apply the script for email messages of the previous day? (Note: this script was designed and extensively tested for email messages of the previous month only)

      To answer your question, you need to (at least):
      1. Configure $endDate and $startDate (required for New-MailboxExportRequest cmdlet to export to PST)
      2. Configure $searchStartDate and $searchEndDate (required for Search-Mailbox cmdlet to delete exported email messages)
      3. Update the content of the email message to be sent after the process.

      I have updated (reorganized) the script and the blog post a bit in the hopes that it will be more readable and 'tweakable' in order to use for other purposes.

      I have tried not to miss any key things. Please bare with me if I do. :)

      Delete