views:

804

answers:

2

EDIT ive got it almost, having and error message now that it can't find the exchange DB. I'm running the service on a different server then Exchange so I presume I have to use an UNC path. MyServer01\First Storage Group\Mailbox Database.edb does not work though...



Hi! I'm getting frustrated here, feeling a massive headache coming up and I am nearly at the point to throw my PC out of the window...

This is the issue, I want to create a new user in the Active Directory via Sharepoint, underneath the Sharepoint list runs a worflow which catches the variabeles and sends them to a webservice which creates the user in active directory. This works perfectly, but the user also needs a mailbox. So, what do we do? We set the mail property to the needed e-mail address and find the property "Create Mailbox" .... say whut? where is it? NOOOOOESSS it does not exist anymore, MS decided that it needs to be more complicated and now the only way to do this is using the powershell crap...

So thats why I am here, I googled some info on this and found some code which should do the trick, but, and thats where I am stuck, the webservice does not run on the Exchange server but on a different server, the webservice needsw to connect to the exchange server to run the powershellshizzle... can't find any info 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: 

The machine you are running this application on needs to have the Exchange Management tools installed, so that the Exchange management snap in is available.

The account you are running it on also needs to have administrative privileges for Exchange.

Sam Cogan
+1  A: 

This post explains what's going. Basically, there used to be something in Exch2003 called the RUS which created the mailbox for 'partially provisioned' users. So you could create a user over LDAP, the and the RUS would pick the new user up on its next pass and complete the process by creating the mailbox and fixing up its other AD attributes.

Now in 2007 the RUS is gone, but you can get the same functionality by scheduling some cmdlets to run periodically (eg with the 'at' command) on the Exchange server.

Andrew Strong