Laeeq Qazi's Exchange Server Blog

Exchange Server blog

Create Mailbox on Exchange Server 2013 using C#

Hi,

Creating a mailbox on Exchange 2013 takes same source code as I used on Exchange 2010.


RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

PSSnapInException PSException = null;

PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();

//StringToSecureString is a function defined at the end

SecureString securePwd = StringToSecureString("Passw0rd1");
Command newMailBox = new Command("New-Mailbox");

newMailBox.Parameters.Add("Name", "TestName1");
newMailBox.Parameters.Add("Alias", "TestName1");
newMailBox.Parameters.Add("database", "Mailbox Database 1406738839");
newMailBox.Parameters.Add("Password", securePwd);
newMailBox.Parameters.Add("DisplayName", "TestName1");
newMailBox.Parameters.Add("UserPrincipalName", "TestName1@ad.lab");
newMailBox.Parameters.Add("OrganizationalUnit", "ou=myorg,dc=ad,dc=lab");
newMailBox.Parameters.Add("FirstName", "TestName1");

pipeline.Commands.Add(newMailBox);

Collection<PSObject> result = pipeline.Invoke();

Here is the helper function

public static SecureString StringToSecureString(string str)
{
   SecureString ss = new SecureString();
   char[] passwordChars = str.ToCharArray();

   foreach (char c in passwordChars)
   {
      ss.AppendChar(c);
   }
   return ss;
}

Note: I used VS 2010 for it. If you use any of the Exchange DLLs, like Microsoft.Exchange.Management.dll etc, then you would have to use VS 2010, as these Exchange DLLs have been developed using .Net 4.0, and you cannot add a reference of .net 4 DLL in VS 2008 or lower version of VS.

Very Important: After a healthy discussion on Exchange 2013 Forum on support of Local PowerShell sessions on Exchange 2010\2013, I don’t recommend to use above mentioned approach to create mailboxes on Exchange Server 2013, instead use Remote PowerShell sessions. I would put a post on it in coming days, where I would show how to create a mailbox on Exchange 2010\2013 using Remote PowerShell. Please visit the link mentioned in the first comment below for more details. I would also mention some problems while adopting Remote PowerShell approach like Object Deserialization issue.

Regards,

Laeeq Qazi

Advertisements

October 2, 2012 - Posted by | Exchange 2013 | , , ,

9 Comments »

  1. Hi Laeeq,

    I followed your TechNet handle to here and wanted to point back to the discussion on the Exchange 2013 Forum
    http://social.technet.microsoft.com/Forums/en-US/exchangeserverpreview/thread/2abd343a-96a4-4617-9972-e98f77b43419/?prof=required

    Can you please add a statement that this is an unsupported method to use PowerShell in Exchange 2010 and 2013 since this is not using remote PowerShell.

    Cheers,
    Rhoderick

    Comment by Rhoderick Milne | October 25, 2012 | Reply

    • Hi Rhoderick,

      I would add a new post here for creating a mailbox using Remote Powershell method on Exchange 2010 and 2013. Also, I have given my comments in the forum thread, that Local powershell is unsupported method in Exchange 2010\2013 but it currently works. You have posted good comments that

      “Not Supported” does not equal “Does not work”

      I really appreciate that some MSFTs have taken interest in this forum thread.

      Thanks and Regards,

      Comment by exchangegeek | October 26, 2012 | Reply

    • Hi Rhoderick,

      I added few lines to this post, to mention that Local PowerShell sessions is not supported method to manage exchange server programmatically on Exchange Server 2010\2013.

      Thanks and Regards,

      Comment by exchangegeek | November 5, 2012 | Reply

  2. I’m really impressed along with your writing skills and also with the structure in your weblog. Is this a paid topic or did you modify it your self? Either way stay up the excellent high quality writing, it is uncommon to peer a great blog like this one today..

    Comment by homemade christmas ornament | December 9, 2012 | Reply

    • Hi,
      Thanks for the compliments, its my personal blog not a paid one, and I put my personal experiences here.

      Kind Regards,

      Comment by exchangegeek | December 9, 2012 | Reply

  3. Hi, Thanks for post. Actually I was using same code for Exchange 2010 developed using VS 2008. but when i build my application using .net 4.0 it stopped working (used nant for building application). But .Net 3.5 built application works perfectly. Same code but target .Net framework is different.
    Exception msg is :”Value cannot be null. Parameter name: serverSettingsValue cannot be null.”

    Comment by Sagar | April 22, 2013 | Reply

  4. Do you have a sample to connect to exchange 2013 on server 2012?

    I’ve tried everything. I did the enable-psremoting, made sure user was enabled for remote powershell, firewalls off, tried basic and Kerberos

    I just keep getting Access Denied when trying from C#

    I am doing this with a web application and have management framework 3.0 installed on the 2008 r2 server where the website is

    Comment by jacobddixon | June 14, 2013 | Reply

    • Hi Jacob,

      Did you resolve your issue?

      Comment by exchangegeek | December 25, 2013 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: