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

October 2, 2012 Posted by | Exchange 2013 | , , , | 9 Comments

No snap-ins have been registered for Windows PowerShell version 3

Hi,

I was developing an app to manage exchange mailboxes and having the error when running the app in VS 2010.

No snap-ins have been registered for Windows PowerShell version 3

It turned out that I was targeting x86 platform in project properties, and I think due to which app was trying to pick 32 bit powershell. I changed the target cpu to “Any CPU” and then error disappeared.

Also please look at http://blogs.msdn.com/b/pareshj/archive/2010/07/30/error-msg-no-snap-ins-have-been-registered-for-windows-powershell-version-2.aspx

Hope it helps someone else too.

Note: To develop apps for Exchange 2013 preview using Exchange PowerShell sdk, you would have to use VS 2010, becuase Exchange server 2013 have been developed using .net 4.0, and you cannot add .net 4.0 DLL references in VS 2008 or lower versions.

Regards,

Laeeq Qazi

October 2, 2012 Posted by | Exchange 2010, Exchange 2013 | , | 4 Comments