Create mailbox for user via webservice on remote Exchange 2007 server. (C #)
EDIT Yves got it almost by having and error message now that he cannot find the exchange DB. I start the service on another server, then Exchange, so I assume I need to use a UNC path. MyServer01 \ First storage group \ Database.edb mailbox is not working though ...
Hello! I'm getting frustrated here, feeling a huge headache and I'm almost ready to throw my computer out of the window ...
This is a problem, I want to create a new user in Active Directory via Sharepoint, a thread is started under the Sharepoint list that catches the variables and sends them to the web service that creates the user in the active directory. This works great, but the user also needs a mailbox. So what should we do? We set the mail property to the desired email address and found the "Create mailbox" property .... say what? where is it? NOOOOOESSS doesn't exist anymore, MS decided it should be more complicated and now the only way to do it is to use powershell shit ...
That is why I am here, I was looking for some information on this and found some code that should do the trick, but, and this is where I am stuck, the web service does not start on the Exchange server, and on another server, the webservice needsw to connect to the exchange server for running powershellshizzle ... can't find information on this, can't find any examples and so on ...
hlep ... F1 ... etc.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
using Microsoft.PowerShell.Commands;
using System.Web.Services;
using System.DirectoryServices;
namespace WebService1
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public void CreateADUser(string domain, string domainPostFix, string firstName, string emailaddress, string lastName, string department, string loginName, string password)
{
DirectoryEntry AD = null;
DirectoryEntry NewUser = null;
AD = new DirectoryEntry("LDAP://OU=Users,DC=" + domain + ",DC=" + domainPostFix);
NewUser = AD.Children.Add("CN=" + firstName + " " + lastName, "user");
NewUser.Properties["samAccountName"].Add(loginName);
NewUser.Properties["name"].Add(firstName + " " + lastName);
NewUser.Properties["displayname"].Add(firstName + " " + lastName);
NewUser.Properties["givenName"].Add(firstName);
NewUser.Properties["sn"].Add(lastName);
NewUser.Properties["userprincipalname"].Add(loginName + "@" + domain + "." + domainPostFix);
NewUser.CommitChanges();
NewUser.Invoke("SetPassword", new object[] { password });
NewUser.CommitChanges();
// E-mail shizzle, don't understand it yet, hopefully it works, if not, don't blame me -Erik
RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();
PSSnapInException PSException = null;
PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out PSException);
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
Command command = new Command("New-Mailbox");
command.Parameters.Add("Name", "TestName");
//Enabling user account
int val = (int)NewUser.Properties["userAccountControl"].Value;
NewUser.Properties["userAccountControl"].Value = val & ~0x2;
NewUser.CommitChanges();
NewUser.Close();
}
a source to share
This post explains what is happening. Basically, in Exch2003 there was something like a RUS that created a mailbox for "partially wealthy" users. So you can create a user via LDAP and RUS will pick up the new user on the next pass and finish the process by creating the mailbox and fixing its other AD attributes.
Now in 2007 RUS is gone, but you can get the same functionality by scheduling some cmdlets to run (for example, using the "at" command) on the Exchange server.
a source to share