Create Exchange 2010 mailbox with C# (powershell)

hi,
i am trying to implement a webapplication (asp.net, c#) where i can create a exchange 2010 mailbox by clicking on a button (simplified szenario).

I think the only way to do this is via powershell. But:

1) do i really need to install powershell on the webserver
2) how do i implement remote powershell (exchangeserver != webserver) in C#
3) do i need to install the exchange management tools on the webserver or does this work via remoting (implicit remoting)
      --> as far as i know i have to load a "exchange snap in" in order to create  mailboxes with powershell
4) has anyone has a little helpful samplecode?

Thank you for suggestions!
wenatexxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raheem05Commented:
Hello,

I would advise you begin at Glen Scales Exchange Blog he is by far one of the best Exchange MVP's I have come accross and well scripting is his art...I hope this is useful!

http://gsexdev.blogspot.com/

wenatexxAuthor Commented:
hi, raheem

thank you for your advise. I had a look at his blog and found some new "potential" approaches.

1) Glen often uses EWS (Webservice for Exchange). But unfortunately i couldn't find a Method like "Create Mailbox" in the dokumentation of the WS :(
2) Furthermore I've seen that for previous releases it was possible to create user mailboxes via active directory using a COM-Object called CODEX.
   Codex is no longer supported for Exchange 2010...

Generally speaking i am a little bit confused, so much possibilities?? no solutions ...
Raheem05Commented:
Hello,

Agree its not supported by EWS for 2010, The only way I can see this being achieved is powershell and yes remote powershell should cut it the box exchange is setup on will have remote powershell...

I am assuming you have seen Nick's blog also?

http://knicksmith.blogspot.com/2007/03/managing-exchange-2007-recipients-with.html

Defend Against the Q2 Top Security Threats

Were you aware that overall malware worldwide was down a surprising 42% from Q1'18? Every quarter, the WatchGuard Threat Lab releases an Internet Security Report that analyzes the top threat trends impacting companies worldwide. Learn more by viewing our on-demand webinar today!

wenatexxAuthor Commented:
Ok,
after reading a lot of blogs and MSDN stuff my code is working.

I am using powershell 2.0 and WinRm to get a remote runspace on the exchange server.

All in all, the hardest challange was some configuration at the exchange server regarding winrm and IIS.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BusbarSolutions ArchitectCommented:
can you post the code snippest that you used to get a remote connect to exchange
wenatexxAuthor Commented:
Here is the code for you.
(Creating a Mailbox for an AD-User)
i think this is only working with exchange 2010.
            SecureString password = new SecureString();
            string str_password = "pass";
            string username = "userr";

            string liveIdconnectionUri = "http://exchange.wenatex.com/Powershell?serializationLevel=Full";

            
            foreach (char x in str_password)
            {
                password.AppendChar(x);
            }
            

            PSCredential credential = new PSCredential(username, password);

            // Set the connection Info
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
            credential);

            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

            // create a runspace on a remote path
            // the returned instance must be of type RemoteRunspace

            Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
            
            PowerShell powershell = PowerShell.Create();
            PSCommand command = new PSCommand();
            


            command.AddCommand("Enable-Mailbox");
            command.AddParameter("Identity", usercommonname);
            command.AddParameter("Alias", userlogonname);
            command.AddParameter("Database", "MBX_SBG_01");
            

            
            powershell.Commands = command;
            try
            {
                // open the remote runspace
                runspace.Open();
                // associate the runspace with powershell
                powershell.Runspace = runspace;
                // invoke the powershell to obtain the results
                return = powershell.Invoke();
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
            finally
            {
                // dispose the runspace and enable garbage collection
                runspace.Dispose();
                runspace = null;
                // Finally dispose the powershell and set all variables to null to free
                // up any resources.
                powershell.Dispose();
                powershell = null;
            }

Open in new window

BusbarSolutions ArchitectCommented:
Thanks a lot, I really appreciate it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.