Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

Remotely create mailbox for new user in Exchange 2010 using PowerShell and C#

$
0
0

I'm currently involved in a Project to automate certain high volume house keeping activities performed by System Admins on Exchange servers, 2007 and 2010.

The challenge I faced at the onset was on deciding how to tackle cmdlets that would have to be called remotely. It took a little research to gather that my search for a solution to this task would involve PowerShell cmdlets that would be run from Domain connected remote workstations that had the RSAT tools installed. However, this would mean, I would have only one option on my hand, ie, to discount the idea of scaling the 'remote' cmdlet solution, for Exchange 2007 as well.

The reason: Exchange 2007 cannot be remotely managed using Powershell. However, with the advent of Exchange Server 2010, we can now run PowerShell cmdlets from remotely connected systems as long as they are part of the same Domain; and certain conditions met. I'll talk about these required conditions later in this article.

Below is the code: (Make sure to add a reference to 'System.Management.Automation")

            SecureString password = new SecureString();
            string str_password = TextBox2.Text;
            string username = TextBox3.Text;
            string cUri = TextBox5.Text;
            foreach (char x in str_password)
            {
                password.AppendChar(x);
            }
            PSCredential credential = new PSCredential(username, password);
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://" + cUri + "/powershell?serializationLevel=Full"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            Command c = new Command("new-mailbox");
            c.Parameters.Add("Database", "Mailbox Database 1499472923");
            c.Parameters.Add("Name", "Tom" + " " + "Bun");
            c.Parameters.Add("Alias", "tbun");
            c.Parameters.Add("OrganizationalUnit", "Permanent");
            c.Parameters.Add("UserPrincipalName", "tbun@myserengati.com");
            SecureString pwd = new SecureString();
            string p = "owlmaster";
            foreach (char z in p)
            {
                pwd.AppendChar(z);
            }
            c.Parameters.Add("Password", pwd);
            bool rp = False;
            c.Parameters.Add(new CommandParameter("ResetPasswordOnNextLogon", rp));
            pipeline.Commands.Add(c);
            try
            {
                pipeline.Invoke();
                TextBox1.Text = "Done!";
            }
            finally
            {
                pipeline.Dispose();
                runspace.Dispose();
                runspace = null;
            }

Following conditions have to be met before running the above, on the server having the CAS role:

> You should be a Domain Admin and a Local Admin

> You should be part of the following Roles in Exchange 2010: Organization Management and Recipient Management

> In IIS, SSL settings should be Unchecked

> Run "winrm quickconfig" from an elevated Command Prompt; and enable Remote Management

> Run "winrm set winrm/config/client @{TrustedHosts="<local>"}" from an elevated Command Prompt. Make sure to modify <Local> to your workstation IP or name

> Enable Basic authentication from local Group Policy

That's it. Now, run it and enjoy!

 

Disclaimer: The information provided herein is provided "AS IS" with no warranties, expressed or implied, nor has been thoroughly tested for a particular purpose, not to mention any other issues, related or un-related, that could arise out of using it. It is highly recommended to test the code in a controlled test environment so it exceeds your expected levels. Please use it at your own risk.


Viewing all articles
Browse latest Browse all 36188

Trending Articles