Avatar of John Darby
John Darby
Flag for United States of America

asked on 

Create mailboxes for all users script

I have a beta script I am wanting to recurse through all domain users (domain = mydomain.com), creating a mailbox for each. Can you help me with the script I have, thus far?

' ------ SCRIPT CONFIGURATION ------
strDCName = "DCone"
strUserName = "CN=<userCN>"       '???NEED to recurse this value, looking at entries in an external CSV file, column one
' ------ 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
VB ScriptExchange

Avatar of undefined
Last Comment
RobSampson

8/22/2022 - Mon