Hello All,
We are prepping for a 2013 upgrade. I am reviewing code for our account creation programs that I wrote in preparation for 2013. Here is a link I found but they have stated that is not the preferred or supported method for mailbox creation for Exchange 2013.
Here is the code I am using on our 2007 Implementation that we will be moving away from: based on what I can see it looks like it will work, but it's essentially the same as what's described above.
public void CreateUserMailbox(string identity, string alias) { //StreamWriter errorLog = new StreamWriter(@"c:\scripts\logs\AutoAddUsersLog.txt", true); ArrayList database = new ArrayList(); ICollection<PSObject> results; // Create a runspace. We can't use the RunspaceInvoke class this time // because we need to get at the underlying runspace to explicitly // add the commands. RunspaceConfiguration rc = RunspaceConfiguration.Create(); PSSnapInException snapEx = null; PSSnapInInfo info = rc.AddPSSnapIn( "Microsoft.Exchange.Management.PowerShell.Admin", out snapEx); Runspace myRunSpace = RunspaceFactory.CreateRunspace(rc); myRunSpace.Open(); // Create a pipeline... Pipeline pipeLine = myRunSpace.CreatePipeline(); Pipeline pipeLine2 = myRunSpace.CreatePipeline(); Pipeline pipeLine3 = myRunSpace.CreatePipeline(); Pipeline pipeLine4 = myRunSpace.CreatePipeline(); using (pipeLine) { // Create a command object so we can set parameters for selecting the database. Command getDatabase = new Command(@"c:\scripts\ADScripts\createusers\Exchange\database.ps1"); pipeLine.Commands.Add(getDatabase); pipeLine.Invoke(); pipeLine.Commands.Clear(); // Create new StreamReader to read the file filled with users to the ArrayList StreamReader readTxt = new StreamReader(@"c:\temp\db.txt"); int i = 0; while (readTxt.EndOfStream != true) { database.Add(readTxt.ReadLine()); } readTxt.Close(); } string[] databaseInfo = database[3].ToString().Replace(@"\", ",").Split(','); string databaseServer = databaseInfo[0]; string databaseName = databaseInfo[1] + @"\" + databaseInfo[2]; databaseServer = databaseServer.Trim(); databaseName = databaseName.Trim(); using (pipeLine2) { // Create a command object so we can set some parameters // for this command. Command newMbx = new Command("Enable-Mailbox"); newMbx.Parameters.Add("Identity", @identity); newMbx.Parameters.Add("Alias", alias); newMbx.Parameters.Add("Database", @databaseServer + @"\" + databaseName); // Add the command we've constructed pipeLine2.Commands.Add(newMbx); // Execute the pipeline and save the objects returned. results = pipeLine2.Invoke(); // Print out any errors in the pipeline execution // NOTE: These error are NOT thrown as exceptions! // Be sure to check this to ensure that no errors // happened while executing the command. if (pipeLine2.Error != null && pipeLine2.Error.Count > 0) { errorLog2.WriteLine("ERROR: There were pipeline errors...\n"); Trace.WriteLine("ERROR: There were pipeline errors...\n"); foreach (object item in pipeLine2.Error.ReadToEnd()) { errorLog2.WriteLine("Error: " + item.ToString() + Environment.NewLine); Trace.WriteLine("Error: " + item.ToString() + Environment.NewLine); } } // Print out the results of the pipeline execution if (results != null && results.Count > 0) { errorLog2.WriteLine("MAILBOXES CREATED: Created the following users...\n"); Trace.WriteLine("MAILBOXES CREATED: Created the following users...\n"); foreach (PSObject ps in results) { if (ps.Members["UserPrincipalName"].Value != null) { errorLog2.WriteLine("UserPrincipalName: " + ps.Members["UserPrincipalName"].Value + Environment.NewLine); Trace.WriteLine("UserPrincipalName: " + ps.Members["UserPrincipalName"].Value + Environment.NewLine); } } } } Thread.Sleep(10000); using (pipeLine3) { // Modify Mailbox pipeLine3.Commands.Clear(); Command set1 = new Command("Set-CASMailbox"); set1.Parameters.Add("Identity", identity); set1.Parameters.Add("IMAPEnabled", false); set1.Parameters.Add("POPEnabled", false); pipeLine3.Commands.Add(set1); pipeLine3.Invoke(); } Thread.Sleep(10000); using (pipeLine4) { // Set Retention Policy pipeLine4.Commands.Clear(); Command mailRetention = new Command("Set-Mailbox"); mailRetention.Parameters.Add("Identity", identity); mailRetention.Parameters.Add("ManagedFolderMailboxPolicy", @"Mailbox Retention Policy"); mailRetention.Parameters.Add("ManagedFolderMailboxPolicyAllowed"); pipeLine4.Commands.Add(mailRetention); pipeLine4.Invoke(); } pipeLine = null; pipeLine2 = null; pipeLine3 = null; pipeLine4 = null; myRunSpace.Close(); myRunSpace = null; }
The code above may not be the prettiest or most efficient implementation -- I'm not a programmer by trade, I'm a Windows/Citrix/Storage/VMware admin, who happens to write code.
My question is, what is the supported method for doing this programmatically, if there is one, and if there isn't what are other people doing to accomplish this? I have to have a way to do this via a program as this code replaced two FTEs on our User Provisioning team and has essentially accomplished a 100% reduction in human error (assuming what is placed in Lawson is correct).
Thanks!