How can I programmatically create an Exchange 2010 mailbox using C #

I was tasked with writing a program to automatically create an Exchange 2010 mailbox. My research tells me to use powershell, but I cannot find the namespace for the link and would like to get some code. I found the code on the Internet, but I donโ€™t know what the namespace is for PowerShell. I think it might be System.Management.Automation, but when I try to reference a namespace, it does not exist in the dotnet list. All I have is System.Management and System.Management.Instrumentation.

Any help would be appreciated?

+4
source share
2 answers

When I did this, I had to download Powershell separately, not sure if this is still the case. You can get it from here .

Here is an example of the code that will create the mailbox:

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; } 
+4
source

This is an old question, but it can help future visitors ...

W69rdy's answer did not help me. But I got his job and reported it here http://pedroliska.wordpress.com/2011/07/22/running-exchange-management-shell-commands-powershell-with-c/

+1
source

All Articles