Guy’s Scripting Ezine 13 – Creating Users

Guy’s Scripting Ezine No 13 – Creating Users with VBScript

Contents of Creating Users with VBScript

This Week’s Secret

I am on the ‘horns of a dilemma’; how long should I make this Ezine?  There is much that I want to say about LDIFDE and even more that I want tell you about VBScript.  One solution is to create an ebook called ‘How to Create Users using VBScript’.  The other solution is to make the Ezine longer.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Using a VBScript to Create Users

VBScript is so versatile and widespread that it is becoming a required skill for Window Server 2003 and 2000 administrators. My point is that tips and techniques you learn when creating users can be applied to other areas, such as, scripts for interrogating the Event Logs, or scripts for changing the registry.

Our goal this week is to build a script that will create new users in your Active Directory domain.

Method 1 – Reading properties from a spreadsheet

Method 1 works like mail-merge.  An Excel spreadsheet holds the data, while a VBScript reads the users’ properties, and then creates the user object in Active Directory.  A bonus of this method is that spreadsheet acts as a record for who you have added to your domain.

The script needs a ‘Loop’ to read the properties from each row and so generate a succession of new users. I have a section in the ebook explaining Do… Until Loop routines.

The crucial feature of the spreadsheet is the first row which holds the LDAP properties for the users. This similar to creating field names for a database.  A key skill is to deduce the LDAP names which correspond to the user properties; for example the LDAP name for First Name is givenName.

Ebook – How to create users with a VBScript

Now, I have a brand new ebook which has a great script to create users.  What you get in the ebook are step-by-step instructions to customize the example script to suit your own domain.  You also get a excel spreadsheet with LDAP properties in the first row. The ebook is divided into 10 sections each page explains a feature of the script in detail.Check out the ebook content here.

Method 2 – Self contained script to create users.

Each week I like to include at least one script for you to copy, paste and modify. The strength of this week’s script is that it will create users in any domain. Its statements are designed to get you started in your quest to script new user accounts. One weakness is that it does not read from a spreadsheet, whereas the script in the ebook opens excel and extracts the LDAP properties.

‘ VBScript to create an OU called BulkGuy
‘ VBSCript then creates 20 Users in OU BulkGuy
‘ Guy Thomas – January 2004

Dim objRoot, objDomain, objOU, objContainer
Dim strName
Dim intUser

strName =”BulkGuy”

Set objRoot = GetObject(“LDAP://rootDSE”)
Set objDomain = GetObject(“LDAP://” & objRoot.Get(“defaultNamingContext”))

Set objRootDSE = GetObject(“LDAP://rootDSE”)
Set objOU=objDomain.Create(“organizationalUnit”, “ou=BulkGuy”)
objOU.Put “Description”, “Guy’s Bulk Users OU”
objOU.SetInfo

Set objContainer = GetObject(“LDAP://OU=BulkGuy,” & _
objRootDSE.Get(“defaultNamingContext”))

For account = 1 To 20
Set objLeaf = objContainer.Create(“User”, “cn=” & strName & account)
objLeaf.Put “sAMAccountName”, strName & account
objLeaf.SetInfo
intUser = intUser +1
Next

WScript.Echo intUser & ” Users created “
WScript.quit

Learning Points

  • On line 9, just after the variables are declared, is the statement: strName = “BulkyGuy”; perhaps you would like to change this string to reflect your name.
  • The reason the script works in any domain is the command: GetObject(“LDAP://rootDSE”). This statement means – use LDAP to get the root domain.
  • For account = 1 To 20. You could amend this to create more, or less accounts. Note the ‘Next’ later in the script which makes the routine loop, and this what creates multiple users.
  • Technically, cn and sAMAccountName are the only two mandatory properties for the user object. With Method 1, it is easier to add more user properties, such as sn = Last Name.

 

See more on how to create a user with a VBScript

LDIFDE – Special Teams Player.

You may remember that last week I mentioned 6 ways of creating scripts?

  1. CSVDE – Solid versatile.
  2. LDIFDE – Fringe or special teams player.
  3. DS Add – New kid on the block.
  4. ADC agreements – The specialist. Here is a tool for migrating from Exchange 5.5 to Active Directory.
  5. Active Directory Users and Computers – A reliable workhorse, but a bit of a plodder if you have lots of accounts to create.
  6. VBScript – Wise old sage, very versatile.

This week’s focus is on the benefits and weaknesses of LDIFDE. If it were in my football team I would have LDIFDE in my ‘Special Team’ unit.

Firstly the acronym LDIFDE means LDIF Data Exchange. If your wondering what LDIF stands for it is – Lightweight Data Interchange Format. In a nutshell the acronym is telling us that this program is about moving data between systems which store data in different structures.

LDIFDE is much like CSVDE. Both are built-in commands in Server 2003 and Windows 2000, and both need a file to exchange data with Active Directory.

The killer advantage of LDIFDE is that you can modify or delete existing objects. Another advantage is that LDIFDE will let you change users passwords.

The disadvantage of LDIFDE is that it does not ‘feed off’ spreadsheets, and I find the format a bit clunky. See more about LDIFDE here.

Summary

VBScript is becoming an essential skill for anyone who has to look after Windows 2000 or 2003 servers.  In this Ezine we have a great example of a VBScript to create users in Active Directory.

One our weekly theme of running through all the available tools, we have a quick look at LDIFDE.

See More Active Directory VBScripts to Create Users

• User Spreadsheet  • Ezines  •LDAP Properties  • ADSIEdit  • Free Solarwinds CSV Import Tool

Ezine 13 Create Users  • Ezine 21 Create Users  • Ezine 23 Enable Accounts  •Ezine 93 ADSIEdit

Ezine 134 Delete Users  • VBScript create users  •PowerShell Get Users  •PowerShell Create Users