Recipe 5.2. Creating a Mailbox for an Existing UserProblemYou have an existing account that isn't mailbox-enabled; you need to create a mailbox for it. SolutionUsing a graphical user interface
Figure 5-1. The Create Mailbox pageUsing a command-line interfaceUse exchmbx with the -cr switch to specify the server, storage group, and mailbox database where you want the user's mailbox to be created: > exchmbx -b <userDN> -cr <server>:<storageGroup>:<mailboxDatabase> Using VBScript' This code adds a mailbox in the first MDB on the server to an ' existing user object ' ------ SCRIPT CONFIGURATION ------ strDCName = "<DC>" ' e.g., "batman" strUserName = "CN=<userCN>" ' e.g., "Random User" ' ------ END CONFIGURATION ------ ' get the default and config NC names Set oIADS = GetObject("LDAP://RootDSE") strDefaultNC = oIADS.Get("defaultnamingcontext") strConfigNC = oIADS.Get("configurationNamingContext") strContainer= "/CN=Users," & strDefaultNC Set objContainer = GetObject("LDAP://" & strDCName & strContainer) ' find the target user Set oIADSUser = GetObject("LDAP://" & strUserName & ",CN=Users," & strDefaultNC) Set oMailBox = oIADSUser ' Open the Connection. Set oConnection = CreateObject("ADODB.Connection") set oCommand = CreateObject("ADODB.Command") Set oRecordSet = CreateObject("ADODB.Recordset") oConnection.Provider = "ADsDSOObject" oConnection.Open "ADs Provider" ' Build the query to find the private MDBs. Use the first one if any are found. strQuery = "<LDAP://" & strConfigNC & _ ">;(objectCategory=msExchPrivateMDB);name,adspath;subtree" oCommand.ActiveConnection = oConnection oCommand.CommandText = strQuery Set oRecordSet = oCommand.Execute If Not oRecordSet.EOF Then oRecordSet.MoveFirst firstMDB = CStr(oRecordSet.Fields("ADsPath").Value) Else firstMDB = "" End If ' create the mailbox oMailbox.CreateMailbox firstMDB oIADSUser.SetInfo WScript.Echo "Created mailbox for " & strUserName DiscussionCreating a mailbox for an account that already exists is relatively straightforward; actually, Microsoft only supports using ADSI or the CDOEXM CreateMailbox method to create the mailbox. That's because mailbox creation actually has four distinct phases:
The reason you need to understand this process is timing. Until step 4, the user can't log on to the mailbox because it hasn't been created. For that reason, many organizations want to automate their mailbox provisioning by setting the security descriptor and other attributes manually; that way, instead of waiting for steps 3 and 4 to finish, users can log on and use their mailboxes immediately after creation. (Actually, these scripts normally send mail to the users as part of the setup process, so step 4 really does occur.) MS KB 304935 describes the somewhat involved process required to set mailbox rights on a newly created mailbox; it's far simpler and safer to let the RUS and store do the work instead, so that's the approach we present here. See AlsoRecipe 5.1 for creating a user and mailbox together, MS KB 313420 (How To Add a Mailbox to an Existing User Account in Exchange 2000 Server), MS KB 275636 (Creating Exchange Mailbox-Enabled and Mail-Enabled Objects in Active Directory), MS KB 327079 (How to programmatically create a mailbox for an existing user in the Active Directory by using CDOEXM), MS KB 304935 (How to set Exchange 2000 mailbox rights at the time of mailbox creation), and MSDN: IMailboxStore::CreateMailbox; exchmbx documentation at joeware.net |