Exchange 2010 - Enable Mailbox through C# & PowerShell

The following is an example on how to use C# to create exchange mailboxes for Exchange 2010 using remote PowerShell.

Imports, not all are used in the example function below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.EnterpriseServices;
using System.Configuration;
using System.Security;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using ActiveDs;
using System.Net;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Security.AccessControl;
/// <summary>
/// Enables and creates a mailbox for an existing user account.
/// </summary>
/// <param name="system">The domain of the account</param>
/// <param name="userid">The account name (samAccountName)</param>
/// <param name="database">The name of the mailbox database.</param>
/// <param name="exchangeRemoteServer">The URL for exchange powershell: https://exchange.subdomain.domain.org/powershell</param>
/// <returns>True or False if successfull</returns>
public bool EnableMailbox(string system, string userid, string database, string exchangeRemoteServer)
{
 try
 {
  // Assumes the process will run with proper priviledges to create mailboxes through powershell.
  WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
   new Uri(exchangeRemoteServer),
   "http://schemas.microsoft.com/powershell/Microsoft.Exchange", // This shouldn't change
   PSCredential.Empty
   );
  // Skip Certificate Verification
  connectionInfo.SkipCACheck = true;
  connectionInfo.SkipCNCheck = true;
  connectionInfo.SkipRevocationCheck = true;
  connectionInfo.AuthenticationMechanism = AuthenticationMechanism.NegotiateWithImplicitCredential;
  using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
  {
   using (PowerShell powershell = PowerShell.Create())
   {
    StringBuilder psScript = new StringBuilder("");
    psScript.AppendLine("Set-ADServerSettings -ViewEntireForest $true");
    if (string.IsNullOrEmpty(database))
    {
     psScript.AppendLine("[bool](Enable-Mailbox -Identity \"{0}\\{1}\" -Alias \"{1}\")");
     powershell.AddScript(string.Format(psScript.ToString(), system, userid));
    }
    else
    {
     psScript.AppendLine("[bool](Enable-Mailbox -Identity \"{0}\\{1}\" -Database \"{2}\" -Alias \"{1}\")");
     powershell.AddScript(string.Format(psScript.ToString(), system, userid, database));
    }
    runspace.Open();
    powershell.Runspace = runspace;
    Collection<PSObject> results = powershell.Invoke();
    if (results != null)
    {
     if (results.Count > 0)
     {
      mailboxEnabled = (bool)results[0].BaseObject;
     }
     else
     {
      mailboxEnabled = false;
     }
    }
   }
  }
 }
 catch (Exception ex)
 {
  // Log your exception!
 }
 return mailboxEnabled;
}
Mastodon: @[email protected]