Create user and mailbox (VBScript)

This code can be found in Chapter 5 of Exchange Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' This script was originally published in the Exchange Cookbook,
' (http://www.exchangebookcook.com). Written by Paul Robichaux, 
' Missy Koslosky, and Devin Ganger. Redistributed with permission 
' of the publisher, O'Reilly & Associates.

'' This code creates a new user mailbox, then mail-enables
' it by creating a mailbox in the first MDB on the server.

' ------ SCRIPT CONFIGURATION ------
strDCName = "<serverName>"	' e.g. "<DC>"
strUserName = "<user>"		' e.g. "jrandomuser"
strFirstName = "Joe"
strLastName = "Blow"
strPassword = "G0bbeldygook!#"
' ------ END CONFIGURATION ------

Set oIADS = GetObject("LDAP://RootDSE")
strDefaultNC = oIADS.Get("defaultnamingcontext")
strConfigNC = oIADS.Get("configurationNamingContext") 
strContainer= "/CN=Users," & strDefaultNC
Set objContainer = GetObject("LDAP://" & strDCName & strContainer)
Set NewUser = objContainer.Create("User", "cn=" & strUserName)
With NewUser
  .firstName = strFirstName
  .lastName = strLastName
  .Put "sAMAccountName", strUserName 
  .SetInfo
End With

With NewUser
   .AccountDisabled = False
   .SetPassword strPassword
   .SetInfo
End With

' Open the connection.
Set theConnection = CreateObject("ADODB.Connection")
set theCommand = CreateObject("ADODB.Command")
Set theRecordSet = CreateObject("ADODB.Recordset")
theConnection.Provider = "ADsDSOObject"
theConnection.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"

theCommand.ActiveConnection = theConnection
theCommand.CommandText = strQuery
Set theRecordSet = theCommand.Execute
If Not theRecordSet.EOF Then
	theRecordSet.MoveFirst
	firstMDB = CStr(theRecordSet.Fields("ADsPath").Value)
Else
	firstMDB = ""
End If

' create the mailbox
With NewUser
  .CreateMailbox firstMDB
  .SetInfo
End With
WScript.Echo "Mailbox created successfully"

This code has been viewed 3130 times.

New from the creators of TechTasks.com: StatSheet.com