I have a VBS script that creates user accounts (we have to create
hundreds each semester). I read information from a text file.
I've simplified it a bit from what we do and taken out actual
server names. Hopefully I haven't introduced any syntax errors.
Here it is; hope it helps.
=========================================================================
'// AddUsers.vbs - script used to create Windows student accounts
'// this scripts creates the account, puts the account in
'// the Student OU, and sets the expiration date
'//
'// 11/00 (slm)
'//
'// Usage: AddUsers.vbs
'//
'// The program expects a file named NTaccounts.txt to be in the current
'// directory. The first line of the file must contain a date (the
'// last day of the semester) in the format yyyymmdd. The rest of the
'// file must contain lines with the syntax:
'// "full_name:sid:username
![Stick Out Tongue :p :p](/styles/default/custom/smilies/tongue.gif)
assword:class_list
Option Explicit
On Error Resume Next
Err.Clear
'// Set global constants.
Const ForReading = 1
Const ForAppending = 8
Const HOMEDIRS = "\\ServerName1\"
Const PROFILES = "\\ServerName2\Profiles\"
'// Declare variables.
Dim fso, MainLog, UserFile, LogFile
Dim DateStr, Mnth, ExpireDate
Dim Classes, i
Dim ouObj
Dim Info, FullName, UserName, Password, Semester, Comment
Dim NewAccts, Updated, Errors
'// Initialize counters.
NewAccts = 0
Updated = 0
Errors = 0
Indx = 0
'// Open the main log file.
Set fso = CreateObject("Scripting.FileSystemObject")
Set MainLog = fso.OpenTextFile("AddUsers.log", ForAppending, vbTrue)
'// Check if there is a NTacounts file to process.
MainLog.Write(Month(Now) & "/" & Day(Now) & "/" & Mid(Year(Now),3,2) & " -
")
If Not fso.FileExists("NTaccounts.txt") Then
MainLog.WriteLine("No NTaccounts file to process.")
WScript.Quit(0)
End If
MainLog.WriteLine("Processing NTaccounts file.")
'// Open the input user file and create a log file.
Set UserFile = fso.OpenTextFile("NTaccounts.txt", ForReading, vbFalse)
Set LogFile = fso.CreateTextFile("NTaccounts.log")
'// Get expiration date from the first line.
DateStr = Mid(UserFile.ReadLine,1,8)
If Mid(DateStr,1,3) <> "200" Then
WScript.Echo "The first line is not a correct date"
WScript.Quit
End If
Mnth = Mid(DateStr,5,2)
ExpireDate = Mnth & "/" & Mid(DateStr,7,2) + 1 & "/" & Mid(DateStr,3,2)
'WScript.Echo "date string = " & DateStr & "; expire date = " & ExpireDate
If (Mnth = 5) Then
Semester = "Sp"
ElseIf (Mnth = 8) Then
Semester = "Sm"
ElseIf (Mnth = 12) Then
Semester = "Fa"
End If
Semester = " - " & Semester & Mid(DateStr,3,2)
'// Get the object where accounts will be created; the Students OU is used
here.
Err.Clear
'// following line changed for an imaginary domain called at.home.ms.com
Set ouObj = GetObject("LDAP://OU=Students,DC=at,DC=home,DC=ms,DC=com")
If (Err.Number <> 0) Then
LogFile.WriteLine("Unable to contact server; error number = " &
Err.Number)
WScript.Quit(1)
End If
'// Loop for each user in input file.
Do Until UserFile.AtEndofStream
Info = Split(UserFile.ReadLine, ":")
FullName = Info(0)
UserName = Info(2)
Password = Info(3)
Classes = Info(4)
'// Set the comment.
Comment = "Student in " & Classes & Semester
'// Create new user account.
AddTheUser
Loop
LogFile.WriteLine(vbCrLf & "Created " & NewAccts & " new accounts")
LogFile.WriteLine("Updated " & Updated & " accounts")
LogFile.WriteLine("Exceptions on " & Errors & " attempts")
Set ouObj = Nothing
LogFile.Close
UserFile.Close
MainLog.Close
Set LogFile = Nothing
Set UserFile = Nothing
Set MainLog = Nothing
'// AddTheUser routine - this is the code that actually adds each user.
Sub AddTheUser
Dim userObj, CurrentComment
On Error Resume Next
'WScript.Echo "user name = " & UserName & ", password = " & Password &
vbCrLf &_
' "full name = " & FullName & ", classes = " & Classes &
vbCrLf &_
' "comment = " & Comment
Set userObj = ouObj.GetObject("user", "CN=" & UserName)
Select Case Err.Number
Case -2147016656 '// user doesn't exist; create
'WScript.Echo "in create account case; error number = " &
Err.Number
Err.Clear
Set userObj = ouObj.Create("user", "CN=" & UserName)
If Err.Number <> 0 Then
LogFile.WriteLine("Unable to create account " & UserName & "
for " & FullName)
Errors = Errors + 1
Err.Clear
Exit Sub
End If
'-- set attributes
userObj.Put "userPrincipalName", UserName
userObj.Put "samAccountName", UserName
userObj.Put "displayName", FullName
userObj.Put "description", Comment
userObj.Put "profilePath", PROFILES & UserName
userObj.Put "homeDrive", "H:"
userObj.Put "homeDirectory", HOMEDIRS & UserName
'-- attributes are now set, create the account
userObj.SetInfo
'-- once created, the disabled status, expiration date and
password may be
'-- configured
userObj.accountDisabled = vbFalse
userObj.accountExpirationDate = ExpireDate
userObj.setPassword Password
userObj.Put "pwdLastSet", CLng(0)
userObj.SetInfo
LogFile.WriteLine("Created account " & UserName & " for " &
FullName)
NewAccts = NewAccts + 1
Err.Clear
Case 0 '// user exists; update info
'WScript.Echo "in account exists case; error number = " &
Err.Number
'// Change user comment, account disabled flag, and expiration
date.
CurrentComment = userObj.Get("description")
i = InStr(CurrentComment, "Student in")
If (i > 0) Then
Comment = Mid(CurrentComment,1,i-1) & Comment
Else
Comment = CurrentComment & " & " & Comment
End If
userObj.description = Comment
userObj.accountExpirationDate = ExpireDate
userObj.SetInfo
LogFile.WriteLine("Updated account information for " & UserName)
Updated = Updated + 1
Case Else '// unexpected error occurred; make note
'WScript.Echo "in error case; error number = " & Err.Number
LogFile.WriteLine("Error checking for existing account for " &
UserName)
Errors = Errors + 1
Err.Clear
Exit Sub
End Select
Set userObj = Nothing
Err.Clear
End Sub
=========================================================================
Jim said:
Probably an easy question, but I've not been able to find
the answer...
I've got a win2K domain running in native mode.
I'm trying to find a way to simply create a script to
create new users in AD.... I've got 20-30 new users per
week, (high turnover rate) that ALL need to belong to the
same OU's, same everything except of course the username.
is there a way to run a bat file or a script that would
automatically create a couple users with similar
attributes?
many many thanks in advance.
Jim B
--
Sandra L Miller
Windows System Administrator
Department of Computer Science
University of Arizona
"The opinions or statements expressed herein are my own and should not be
taken as a position, opinion, or endorsement of the University of Arizona."