I posted a while back about my PowerShell module called ExchangeLab which includes the New-LabMailbox cmdlet used to populate Exchange labs with mailboxes. I've recently updated this module to include a new cmdlet called Send-LabMailMessage that can be used to generate test email data after your test mailboxes have been created.
Here is an overview of how Send-LabMailMessage works:
- It uses the EWS Managed API, so mail is sent directly through Exchange – this means you don't need to deal with smtp relay and you can let the cmdlet find Exchange using autodiscover.
- It accepts pipeline input from the Get-Mailbox and Get-DistributionGroup cmdlets – you can quickly populate your labs with email data by simply piping recipients to this cmdlet.
- It provides Count and MessageSize parameters that allow you to specify the number of messages to be sent to each recipient and how large the message should be (in KB, MB, GB, etc).
Download the module here. Follow the instructions in my previous post on how to install the module to your modules directory. Once the module is installed and imported into your EMS session, you are ready to get started. Below are a couple examples of how you could use the Send-LabMailMessage cmdlet to generate test email data in your lab.
Send an Email to Every Mailbox
This would send a message 1MB in size to every mailbox:

Send Emails to Distribution Groups
This would send a 25 messages to each member of the Sales distribution group. The default message size is 100kb:

This module will work with both Exchange 2007/2010. If you are using 2007, just make sure you have SP2 and PowerShell v2 and the Microsoft .NET Framework 3.5. By default, the Send-LabMailMessage cmdlet is versioned to Exchange 2010. If you are working with Exchange 2007, set the version parameter to Exchange2007_SP1. Also, remember that this cmdlet uses the EWS Managed API, so the user running the cmdlet must have a mailbox in the Exchange organization.
Update 4/27/10 – I modified the Send-LabMailMessage cmdlet so that the messages are not saved in the sent items folder since this could fill up the senders mailbox very quickly. If you want to re-enable this, change line 218 of the ExchangeLab.psm1 file to: $mail.SendAndSaveCopy()





{ 42 comments… read them below or add one }
hi
maybe im doing something wrong but im getting this error when trying to send mail:
Exception calling “SendAndSaveCopy” with “0″ argument(s): “Exchange server doesn’t support requested version.”
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ExchangeLab\ExchangeLab.psm1:218 char:25
+ $mail.SendAndSaveCopy <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
any help would be appreciated
thanks
sorry about that:(
everything works, ofcourse i forgot im running it on exchange 2007 with the version being exchange 2010 by default
great script thanks
Great, glad it’s working for you.
hey
its me(turbomcp)
i downloaded the new updated script and now im getting this:
[PS] C:1>Get-DistributionGroup “exchange 2007 users” |Send-LabMailMessage -coun
t 10 -MessageSize 500kb -Version Exchange2007_SP1
Exception calling “AutodiscoverUrl” with “1″ argument(s): “The Autodiscover ser
vice could not be located.”
At C:1ExchangeLab.psm1:203 char:29
+ $service.AutodiscoverUrl <<<< ($user.Properties.mail)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
any ideas whats wrong here?
Looks like an autodiscover issue. Can you try manually specifying the EWS url?
for example:
Send-LabMailMessage -count 10 -MessageSize 500kb -Version Exchange2007_SP1 -Url https://mail.contoso.com/ews/exchange.asmx
hey
thanks for fast reply
i saw that autodiscover related error
so i checked my stuff and it was good(the settings for external and internal url for autodiscovery)
but only when i aaded a dns entry for my external domain(autodiscover.test.com) it started working.
so im guessing that if i dont specify anything it takes the externalurl and if i specify it can goto internal one?
thanks again for great script and fast reply
I only added the URL parameter so there is a way to circumvent autodiscover if there is a problem. The autodiscover client queries the SCP object in AD to obtain the URL. It will attempt to resolve using DNS otherwise. So generally, you shouldn't need to provide a URL unless there is a problem.
I case you have problems getting this to work due to SSL errors, a bypass is installing the default machine certificate in Trusted Root Authorities. You might be in this scenario if you got a default setup demo enviroment.
For webcrawlers..The error experienced was:
ForEach-Object : Exception calling “Send” with “0″ argument(s): “Request failed. The underlying connection was closed:
Could not establish trust relationship for the SSL/TLS secure channel.”
Can you go into a little more detail on how you install a default machine certificate in Trusted Root Authorities?
I am getting this error.
[PS] C:\Windows\system32>get-mailbox -ResultSize unlimited | Send-LabMailMessage -MessageSize 30kb -count 50 -version Exchange2007_SP1 -Url https://ex2007.complab.local/ews/exchange.asmx
Exception calling “Send” with “0″ argument(s): “Request failed. The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.”
Thanks.
Here’s an older article, but the steps are the same:
http://blogs.technet.com/b/sbs/archive/2007/04/10/installing-a-self-signed-certificate-as-a-trusted-root-ca-in-windows-vista.aspx
Hello,
I am having issues getting this to work. I tried your string below but I keep getting an error message about an unexpected token.. Any ideas?
[PS] C:\>Send-LabMailMessage -count 1 -MessageSize 500kb -Version Exchange2007_SP1 -Url http://www.yahoo.com
cmdlet Send-LabMailMessage at command pipeline position 1
Supply values for the following parameters:
PrimarySmtpAddress: administrator@sea-demo.com
Exception calling “Send” with “0″ argument(s): “‘–’ is an unexpected token. The expected token is ‘>’. Line 81, positi
on 5.”
At C:\Documents and Settings\Administrator.SEA-DEMO\My Documents\WindowsPowerShell\ExchangeLab.psm1:218 char:14
+ $mail.Send <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Hi Chris,
What about this?
Get-Mailbox administrator | Send-LabMailMessage -count 1 -MessageSize 500kb -Version Exchange2007_SP1
or…
Send-LabMailMessage -PrimarySmtpAddress administrator@sea-demo.com -count 1 -MessageSize 500kb -Version Exchange2007_SP1
I don’t really see why you would be getting that error though…are you pasting the code into the shell? Sometimes there are problems with that, try typing the command from scratch.
Hi Mike,
I’m not sure why it is not working. I tried typing in manually and same issue.. Have you checked or reloaded the module since you made the $mail.SendAndSaveCopy() change to $mail.Send()?
Chris, yes I just downloaded and tested it on both 2007 and 2010, no issues. Are you manually specifying your ews url, and if so is it in the format of “https://mail.yourdomain.com/ews/exchange.asmx”?
Hi Mike,
I was able use the script to create the users but am having problems sending messages with this script. I’ve created a self-signed cert for the test site and am able to access the site but when I run the script I get an error:
Exception calling “Send” with “0″ argument(s): “Request failed. The remote serv
er returned an error: (403) Forbidden.”
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\ExchangeLab\ExchangeLab.p
sm1:218 char:14
+ $mail.Send <<<Get-Mailbox -ResultSize unlimited |Send-LabMailMessage -count 1 -versio
n Exchange2007_SP1 -Url https://outlook.testlab.com/ews/exchange.asmx
Any ideas?
Thanx for putting all this together…..
Garry
Hi Garry. Are you running this from a domain joined machine logged in as a user with an Exchange mailbox? If so, and the self signed certificate is installed in the Trusted Root Certificate Authorities Store on the machine that you are running it from, what happens when you run the following command?
Get-Mailbox -ResultSize unlimited | Send-LabMailMessage -count 1 -version Exchange2007_SP1
Mike, thanx for getting back to me so quick…
I resolved the issue by removing the Web Services Directory then creating a new one, also in the URL switch I put the external URL for autodiscover but that was not set as the external URL for Webservices.
Works great now…
thanx again.
Garry
Great, glad you got it working.
Hi Mike
Awesome Labs tools for Exchange. The best found on the Internet!
Thanks,
Peter
Thanks Peter! Glad you found it useful.
Hi Mike,
I’m running into this error and am at a loss at this point. This is against Exchange 2010 SP1
[PS]
C:\Users\administrator.VAULT9\Documents\WindowsPowershell\Modules>Get-Mailbox
-ResultSize unlimited | Send-LabMailM
essage -MessageSize 1mb -Url
https://Ev-Exch2010.Vault9.local/EWS/Exchange.asmx
ForEach-Object : Exception calling “Send” with “0″ argument(s): “Request
failed. The underlying connection was closed:
Could not establish trust relationship for the SSL/TLS secure channel.”
At
C:\Users\administrator.VAULT9\Documents\WindowsPowerShell\Modules\ExchangeLab\ExchangeLab.psm1:195
char:16
+ 1..$Count | % <<<
I’m not sure if it’s an SSL issue or something to do with where it says “0″ arguments when using “Send”
Regards,
Mike
Hey Mike,
Based on the error, it sounds like you’re still using the self signed SSL certificate on the Exchange server. The easiest way to get around this is to make sure the machine running the code trusts the certificate. This is an older article but it shows you how to install the self signed cert from the server in the local trusted root certificate authority store on your machine:
http://blogs.technet.com/b/sbs/archive/2007/04/10/installing-a-self-signed-certificate-as-a-trusted-root-ca-in-windows-vista.aspx
Hi Mike,
That did the trick! And thanks for the link to the steps on how to do this. That was a good article and very easy to follow.
Mike
Great, glad you got it working Mike and thanks for the follow up
Hey Mike,
Thanks so much for the module. I am coming up with an error and maybe you can help me out:
[PS] C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Exchange Server 2007>Get-DistributionGroup halfgb | Send-LabMailMessage -MessageSize 1mb -Version Exchange2007_SP1
Exception calling “Send” with “0″ argument(s): “Expected XML node type was XmlDeclaration, but actual type is Element.”
At C:\Users\sfrankiel\Documents\WindowsPowerShell\Modules\exchangelab\ExchangeLab.psm1:218 char:14
+ $mail.Send <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
If I recall correctly, this is a known issue with .NET 3.5.1 and the hot fix is at http://support.microsoft.com/kb/958934
Thank you for the quick response!
It seems that isn’t the issue though
. I am running the lab module on workstation and when I run the Send-LabMessage on Exchange itself I get different errors (about Autodiscover). When i specify the full EWS url, as you recommended above, it give me errors about the user not having a mailbox (which is expected from the user I am using on Exchange). Once I resolve the Autodiscover misconfiguration I will report back.
OK, so I think I got the AutoDiscover errors fixed (at least the error message has changed
). Now i am getting the following error:
[PS] C:\Windows\system32>Get-Mailbox -Identity t001 | Send-LabMailMessage -count -Version Exchange2007_SP1
1
Exception calling “Send” with “0″ argument(s): “Request failed. The remote serv
er returned an error: (401) Unauthorized.”
At C:\Users\sfrankiel\Documents\WindowsPowerShell\Modules\ExchangeLab\ExchangeL
ab.psm1:218 char:14
+ $mail.Send <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Do you have Windows Integrated Authentication enabled for the Web Services virtual directory?
Mike,
Yes I do have Integrated Authentication for the EWS directory.
Thanks again for your help with all of this.
Mike, the cmdlet “Send-LabMailMessage” is missing from the package, can you provide?
It’s in the module…make sure you do an import-module exchangelab and the function will be available
Download the module here:
http://www.mikepfeiffer.net/downloads/ExchangeLab.zip
Thanks Mike
I thought I read somewhere on this site that you wouldn’t have to log into each mailbox when using the script? I’m getting the ‘WARNING: The user hasn’t logged on to the mailbox’ warning when running a Get-Mailbox | Get-MailboxStatistic command from like half of the users I created with the plug in. I created 5 users in one OU and 5 more in another OU. The issue happens only with the 5 users in the same OU.
Send them an email message and the warning will go away. You can use the Send-LabMailMessage cmdlet in this module to automate that with a one-liner.
Mike;
This is very nice.
Much more elegant than my method of creating users for my test lab!
Hey Karl, thanks!
Mike,
Have just purchased your MS Exchange 2010 PowerShell Cookbook. On opening I was quickly drawn to Chapter 13 (lucky or unlucky I don’t know). I’ve created ps1 script straight from your txt as below but am coming a cropper on a Proxy Authentication required blip. Is there a way to supply the required in PowerShell and if so, at what stage should i include it in my PS script. Regards Nick Simmonds
Initially I was thinking this might work but I haven’t had success:
#Creates a proxy object and uses the credential I’m running PS as.
$proxy = new-object System.Net.WebProxy(“http://iproxy:80″)
$proxy.UseDefaultCredentials = $true
Actual console error
Exception calling “SendAndSaveCopy” with “0″ argument(s): “The request failed.
The remote server returned an error: (407) Proxy Authentication Required.”
At C:\EWS\TestEWSImpersonation.ps1:34 char:21
+ $msg.SendAndSaveCopy <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
The PS I've jumbled together to send an email on behalf of testuser2@len.com
#Imports the EWS managed APi assembly into the Shell – downloaded and installed into C:\EWS
Add-Type -Path C:\EWS\Microsoft.Exchange.WebServices.dll
#Creates an instance of the ExchangeService class
$svc = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
#Hooks up a certificate validatiaon callback method setting the EWS VDir certificate to good.
$spm = [System.Net.ServicePointManager]
$spm::ServerCertificateValidationCallback = {$true}
#Sets the EWS end-point using AutoDiscover
$svc.AutoDiscoverUrl("impers@len.com")
#Creates an instance of the EmailMessage class
$msg = New-Object -TypeName Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $svc
#Set specific properties on the $msg object
$msg.Subject = "Test E=Mail"
$msg.Body = "This is a test"
$msg.From = "testuser2@len.com"
$msg.ToRecipients.Add("testuser@len.com")
$msg.SendAndSaveCopy()
I can’t test this right now, but give this a shot:
$proxy = new-object System.Net.WebProxy(‘http://iproxy:80‘)
$proxy.UseDefaultCredentials = $true
$svc.WebProxy = $proxy
Ok, got a chance to test it…works fine….
http://www.mikepfeiffer.net/2011/12/how-to-use-a-proxy-server-with-the-ews-managed-api-and-powershell/
Hi Mike,
Excellent work!
I was going crazy with the scripts not working.. met all the above mentioned errors.. did a lot of extra research and tried every piece of advice with no luck.
Until.. it crossed my mind and I did a.. Windows Update. Which of course also updated Exchange to rollup 6.
It all works great now!
Cheers,
Tibi
Mike,
thanks for the great tool. I had most of the issues described above and resolved them by patching the Exchange or using the options. There was this last problem I had with the app not being able to send mail to recipients logging an error with Event ID 10 in the Application Log on my Exchange 2007 server saying that the CAS server attempted to find a CAS server in AD site but was unable to locate it in AD. I found that this was due to me having a mixed Exchange environment in the lab, both 2007 and 2003, and EWS can’t contact Exchange 2003. Once I moved the legacy boxes to the Exchange 2007 server, things finally started working. Just wanted to post it here in case somebody else runs into this.
Thanks again,
Sergey