Skip to main content

Microsoft

Script For Generating Mailbox Test Data Over A Period Of Time

Just a quick but helpful script today for generating mailbox test data. While there are other scripts available that can send test emails to populate a mailbox, in some instances you need those emails to be spread out over a span of time.
If you want to test out archiving, retention policies, OST caching or any other date-sensitive operation, a mailbox full of test data from the day prior is not that helpful.
In the script below, you can populate a test mailbox with emails going back X number of days with Y number of emails per day.

Some Influences…

Some of the code in this script was inspired by examples from Glen Scales and Mike Pfeiffer. Glen is pretty well known as the go-to for all things EWS API related and Mike has a nice script for generating lab mailboxes.

Prerequisites

This script uses version 2.2 of the EWS Managed API. If you don’t have it installed, you can download it from Microsoft: Microsoft Exchange Web Services Managed API 2.2. The API should install in a default folder of “C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll”, if you install it in a different location you will want to modify the script or use the command line parameter to specify the alternate location.
You will also need an account that is setup for Impersonation. If you don’t have an account with this role, you can easily set it up with these instructions: “How To: Configure Impersonation“.

Using The Script

The script takes up to eight command line switches although you can specify as few as one. The parameters are as follows:

TargetMailbox
The SMTP address for the mailbox that you want to populate.
NumDaysBack
The number of days back from the current date to start populating messages. Default value is 120 days.
MsgsPerDay
The number of messages to create for each day. Default value is 5 messages per day.
MsgSize
The size of the attachment for the message. Default value is 100kb.
AutoD
If specified as $True, Autodiscover is used to determine the EWS endpoint. Default value is $True.
EwsUri
EWS endpoint for target mailbox. Default value is to use Office 365 if not using Autodiscover.
ApiPath
Location of EWS API. Default path is “C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll”.
Version
Exchange version to be used by EWS. Default is “Exchange2013_SP1” and likely does not need to be changed unless you’re using an earlier version of Exchange. Acceptable options are documented at “EWS schema versions in Exchange“.

So using the following would give us a mailbox with 3 years of email where there are 10 messages per day of 200 KB each:

.\Populate-TestMailbox.ps1 -TargetMailbox test.user@iwitl.com -NumDaysBack 1095 -MsgsPerDay 10 -MsgSize 200kb

When executing the script, you’ll be prompted for the credentials of the account that has the Impersonation role.

Note

It should be noted that this script does not actually “send” emails as you cannot send the email and fake the date; instead, it creates emails and saves them into the Inbox folder. I have not noticed any difference in the operation of functions like retention policies or OST caching but it’s something to watch for.

Download

The script for this post can be found in the Microsoft Script Center at the following link: Populate-TestMaiboxByDate.ps1

Did you find this article helpful?
Leave a comment below or follow me on Twitter (@JoePalarchio) for additional posts and information on Office 365.
Looking to do some more reading on Office 365?
Catch up on my past articles here: Joe Palarchio.

Thoughts on “Script For Generating Mailbox Test Data Over A Period Of Time”

  1. quick question:
    I have hybrid of 2013 2010 and when I run it against cloud users ,works noprob
    when I try onprem I get this:
    Exception calling “Bind” with “2” argument(s): “Exchange Server doesn’t support the requested version.”
    At C:\Users\administrator.TEST\Downloads\Populate-TestMailboxByDate.ps1:101 char:1
    all the url’s point to 2013 server(autodiscover, web services…)
    any ideas on what I can change to make it work?
    amazing script btw:)

  2. Thanks joe
    tried it and got some traction and got this:
    Exception calling “Bind” with “2” argument(s): “The SMTP address has no mailbox associated with it.”
    At C:\Users\administrator.TEST\Downloads\Populate-TestMailboxByDate.ps1:101 char:1

  3. Hi Guys
    Any idea wht in getting the follwing error.. my setup all looks fine so not sure why the api im calling is giving me the error
    Exception calling “Save” with “1” argument(s): “Value cannot be null.
    Parameter name: parentFolderId”
    At D:\Exch\Populate-data\Populate-TestMailbox.ps1:121 char:9
    + $Message.Save($Inbox.Id)
    + ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

  4. Having the same issue as Pav Bahia:
    Save : Exception calling “Save” with “1” argument(s): “Value cannot be null.
    Parameter name: parentFolderId”
    At C:\Scripts\Populate-TestMailboxByDate.ps1:120 char:22
    + $Message.Save <<<< ($Inbox.Id)
    I have manually specified EwsUri, ApiPath, configured impersonation on my account that I use to run the script, tried to enter credentials both in domain\username and without domain\, tried to use EWS 2.2, then 1.2, changed my version to Exchange2010_SP2 that I use. Nothing helped. Please advise.

  5. Having the same issue as Pav Bahia:
    Save : Exception calling “Save” with “1” argument(s): “Value cannot be null.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Joe Palarchio

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram