Link to home
Start Free TrialLog in
Avatar of wenatexx
wenatexxFlag for Austria

asked on

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!
Avatar of Raheem05
Raheem05
Flag of United Kingdom of Great Britain and Northern Ireland image

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/

Avatar of wenatexx

ASKER

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 ...
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

ASKER CERTIFIED SOLUTION
Avatar of wenatexx
wenatexx
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
can you post the code snippest that you used to get a remote connect to exchange
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

Thanks a lot, I really appreciate it.