Create 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 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

This code has been viewed 4950 times.

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