ron.williams.IT.blog

Am I a Microsoft Infrastructure technology evangelist? I guess so…

VBScript to Create Mail Enabled Users with Populated Mailboxes

with 2 comments

Here is a vbscript that I wrote that will create a specified number of Exchange 2003 mail enabled TestUsers with populated mailboxes.   It really helps when creating (and recreating) lab users to test Active Directory and Exchange migrations in a Proof Of Concept (POC).  It should be run from an Exchange 2003 box in a non-production environment. 
 
Here is what it does:
1.       Creates Root level OU called TestOU
2.       Creates a specified number of users in that OU
3.       Creates mailboxes for all those users (mailbox enabled users)
4.       Waits 60 seconds
5.       Fills each mailbox with a specified number of emails using SMTP
 
Here is the syntax:
cscript scriptname.vbs (Number of Users) (SMTP Namespace specified in Recipient Policies) (Number of Emails to Create) (Name of the mailbox store) (Active Directory Domain Name)
For example, to create 50 users with 10 emails in each mailbox, here is the command:
cscript CreateRecipients.vbs 50 "domain.com" 10 "Mailbox Store (EX2003)" "domain.local"
 
 

‘This script creates an OU called TestOU in the root of the domain.
‘Then it creates the specified number of mailbox enabled users which are not disabled
‘All users have the same password
‘Then it populates their inboxes with the specified number of emails.
‘Always enclose arguments with spaces in quotes,
‘Seperate the arguments with a space
‘Should be run on the exchange 2003 server in a lab environment.
‘For help, email ron dot williams at mail dot com
‘Arguments= (number of test mailboxes)(domain name)(number of test emails to create)(name of mailbox store) (Active Directory Domain Name)
‘an example of the command used to run this script to create 50 users with 10 emails in each mailbox is:
‘cscript CreateRecipients.vbs 50 "domain.com" 10 "Mailbox Store (EX2003)" "domain.local"
 
‘Argument1 = oArgs.Item(0) = number of test users to create
‘Argument2 = oArgs.Item(1) = SMTP namespace domain name of the recipients ie "domain.com" is the domain name for TestUser1@domain.com
‘Argument3 = oArgs.Item(2) = number of test emails to create
‘Argument4 = oArgs.Item(3) = home MDBname, for example "Mailbox Store (EX2003)" this can be found in exchange System Manager next to the mailbox database
‘Argument5 = oArgs.Item(4) = Active Directory Domain Name domain name of active directory "domain.local" This may or may not be the same as the SMTP namespace.
 
 

Set

oArgs=WScript.Arguments

If

oArgs.Count < 5 Then ‘if the script is run with less than 5 arguments, it errors out.

Set objShell = Wscript.CreateObject("Wscript.Shell")

objShell.LogEvent EVENT_TYPE_ERROR, _

"Script was run with an incorrect number of arguments."

wscript.echo

"Script was run with an incorrect number of arguments."

WScript.Quit -1

End

If

 
 

‘get the domain DN from the oArgs.Item(4)argument

DomainDN =

"dc=" & Replace(oArgs.Item(4),".",",dc=")

‘Create the OU "TestOU" to house the user objects
‘on Error Resume Next

Set

objDomain = GetObject("LDAP://" & DomainDN)

Set

objOU = objDomain.Create("organizationalUnit", "ou=TestOU")

objOU.SetInfo
 

‘Create Users

On

Error Resume Next

For

CountUsers = 1 to oArgs.Item(0)

EmailName =

"Test_User" & CountUsers ‘ this will be the user principle name TestUser1 TestUser2 etc

FirstName =

"Test_"

LastName =

"User" & CountUsers

 

‘ Bind to Active Directory, TestOU container.

Set objRootLDAP = GetObject("LDAP://rootDSE")

Set objContainer = GetObject("LDAP://OU=TestOU," & _

objRootLDAP.Get(

"defaultNamingContext"))

‘ Create the actual User.

Set objNewUser = objContainer.Create("User", "cn=" & emailname)

objNewUser.Put

"sAMAccountName", EMailName

objNewUser.Put

"sn", LastName

objNewUser.Put

"givenName", FirstName

objNewUser.Put

"userPrincipalName", emailname

objNewUser.SetInfo
objNewUser.SetPassword

"Password1"

objNewUser.AccountDisabled =

False

objNewUser.SetInfo

Next

 
wscript.echo

"Created " & oArgs.Item(0) & " users."

 

‘The next section creates Mailboxes for all users in TestOU

strOU =

"OU=TestOU"

strStore = oArgs.Item(3)

Set NC = GetObject("LDAP://RootDSE")

Set oIADS = GetObject("LDAP://RootDSE")

strConfContext = NC.Get(

"defaultnamingcontext")

strADSPath =

"LDAP://" & strOU & "," & oIADS.Get("defaultNamingContext")

Set objCommand = CreateObject("ADODB.Command")

Set objConn = CreateObject("ADODB.Connection")

objConn.Open

"Provider=ADsDSOObject;"

Set objCommand.ActiveConnection = objConn

objCommand.CommandText =

"SELECT distinguishedName FROM "+"’"+strADsPath+"’"+" WHERE objectClass = ‘user’"

objCommand.Properties(

"searchscope") = 2

objCommand.Properties(

"Page Size") = 1000

Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF

Set oIADSUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName"))

Set oMailBox = oIADSUser

oMailbox.CreateMailbox FindAnyMDB(

"CN=Configuration," & strConfContext)

oIADSUser.SetInfo
objRecordSet.MoveNext

Wend

 
 

Function FindAnyMDB(strConfigurationNC)

Dim oConnection

Dim oCommand

Dim oRecordSet

Dim strQuery

Set oConnection = CreateObject("ADODB.Connection")

set oCommand = CreateObject("ADODB.Command")

Set oRecordSet = CreateObject("ADODB.Recordset")

oConnection.Provider =

"ADsDSOObject"

oConnection.Open

"ADs Provider"

 
strQuery =

"<LDAP://" & strConfigurationNC & ">;(Name=" & strStore & ");name,adspath;subtree"

oCommand.ActiveConnection = oConnection
oCommand.CommandText = strQuery

Set oRecordSet = oCommand.Execute

If Not oRecordSet.EOF Then

oRecordSet.MoveFirst
FindAnyMDB =

CStr(oRecordSet.Fields("ADsPath").Value)

Else

FindAnyMDB =

""

End If

oRecordSet.Close
oConnection.Close

Set oRecordSet = Nothing

Set oCommand = Nothing

Set oConnection = Nothing

End Function

wscript.echo

"Mailbox enabled " & oArgs.Item(0) & " users."

 

‘wait 60 seconds

cscript.echo

"Waiting for 60 seconds…."

Wscript.Sleep 60000
wscript.echo

"Sending " & oArgs.Item(1) & " emails…"

 

‘Use SMTP to create mass emails to activate and populate the email boxes

For

CountMailboxes = 1 To oArgs.Item(0)

varDestUser =

"Test_User" & CountMailboxes &"@" & oArgs.Item(1)

For CountEmails = 1 To oArgs.Item(2)

set objEmail = CreateObject("CDO.Message")

objEmail.From =

"TestSender@MassEmailScript.com"

objEmail.To = varDestUser
objEmail.Subject =

"Testing Email Number " & CountEmails

objEmail.Textbody =

"Testing Email message body number " & CountEmails

objEmail.Send

Next

Next
 

wscript.echo

"Done! Check Active Directory Users and Computers to verify " _

&

"the existence of the TestOU and the Test_Users. Use Exchange System Manager " _

&

"to verify that mailboxes were created and populated."

 
‘Clear Arguments out of memory

set

oArgs=Nothing

About these ads

Written by r0nwilliams

October 10, 2008 at 1:31 pm

Posted in Active Directory

2 Responses

Subscribe to comments with RSS.

  1. For some reason it does not work. Either I am doing something wrong and there ir code error. I copied the code content as it is and create a VBS file.

    C:\Script>cscript.exe CreateRecipient.vbs 50 “testmail.com” 10 “Mailbox Store (CNEX
    2003MB)” “test.com”
    Microsoft (R) Windows Script Host Version 5.6
    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    C:\Script\CreateRecipient.vbs(1, 4) Microsoft VBScript compilation error: Syntax
    error

    Sandeep Manjrekar

    January 12, 2012 at 1:37 am

  2. For some reason it does not work. Either I am doing something wrong and there ir code error. I copied the code content as it is and create a VBS file.

    C:\Script>cscript.exe CreateRecipient.vbs 50 “testmail.com” 10 “Mailbox Store (CNEX
    2003MB)” “test.com”
    Microsoft (R) Windows Script Host Version 5.6
    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    C:\Script\CreateRecipient.vbs(1, 4) Microsoft VBScript compilation error: Syntax
    error

    Sandeep Manjrekar

    January 12, 2012 at 2:06 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: