PDA

View Full Version : Creating Users (W2K)



djcomplex
06-11-2002, 12:19 PM
i am trying to write a script to add users to active directory, i _think_ it works ok but it just isnt connecting to the domain atm so that isnt a script issue, what i want to be able to do is import a .txt file with a list of users in so it makes the accounts automatically because creating 1200 users accounts one at a time is a no no

this is what i have so far

On Error Resume Next
dim objDS, objUser, wshNetwork
dim WshShell
dim objRoot,objDom

set wshShell=CreateObject("Wscript.Shell")
set wshNetwork=CreateObject("Wscript.Network")

set objRoot=GetObject("LDAP://RootDSE")
set objDom=GetObject("LDAP://" & objRoot.Get("defaultNamingContext"))

shortADSPath=MID(objDom.ADSPath,8)

strDomain=wshNetwork.UserDomain

'Where will the user's home directory be created?
strUsersDir="\\MCS\users$"
strHomeSrv="MCS"
'what is the local path on the server where the share will be created?
strLocalPath="C:\users"

strUser=Trim(InputBox("Enter in the new username","Create User Account","firstname lastname"))
ChkInput err.Number,"You entered a blank user name or cancelled."

numSpace=InStr(struser," ")
strFirst=Left(strUser,Numspace-1)
strLast=Mid(strUser,NumSpace+1,20)
strLogon=Left(strFirst,1)&strLast
strUsername=LCASE(Left(strFirst,1)&strlast)
strDomain="magnacarta.surrey.sch.uk"

strPass=InputBox("Enter in a password","Create User Account",LCase(strLogon))
ChkInput strPass,"You entered a blank password or cancelled."

strYearGroup=InputBox("Enter the Year Group")

strOU=InputBox("Where do you want to create the new user account?","Create User Account","c:/users/"&strYearGroup&"/"&strLogon&"")
set objDS=GetObject(strOU)
ChkErr err.number,"Error connecting to " & strDomain

'uncomment next line for debugging
wscript.echo "Creating account for " & strFirst & " " & strLast & " (" & Left(strFirst,1)&strlast&"@"&strDomain&")"

'Create user object
set objUser=objDS.Create ("User","cn="&strUser)
objuser.Put "samAccountName",strUserName
objuser.Put "UserPrincipalName",strUserName & "@" & strDomain & ""
objUser.SetInfo
ChkErr err.number,"Error setting info for " & strUserName & " on " & strDomain

'Now that user object is created, let's set some properties
'There are plenty of other AD properties you can set. These are the more
'likely ones.
objUser.FirstName=strFirst
objUser.LastName=strLast
objUser.FullName=strFirst & " " & strLast
objUser.Description="Created " & NOW & " by " & wshNetwork.UserName
objUser.EmailAddress=Left(strFirst,1)&strlast&"@"&strDomain&".com"
objUser.AccountDisabled=True
objUser.SetPassword(strPass)
objUser.SetInfo

objUser.HomeDrive="C:"
objUser.HomeDirectory="\\" & strHomeSrv &"\" & strUserName & "$"
'comment out the next line if you don't want to specify a network profile
'location
objUser.Profile="\\mcs\profiles$\" & strUserName
objUser.SetInfo

'Create User Directory
'dim oFileSys
set oFileSys=CreateObject("Scripting.FileSystemObject")
oFileSys.CreateFolder(strUsersDir & "\" & strUserName)

'share the new home directory
dim objNewShare, objSrv
set objSrv=GetObject("WinNT://" & strHomeSrv & "/LanManServer")
set objNewShare=objSrv.Create("fileshare",strUserName & "$")
objNewShare.Path=strLocalPath & "\" & strUserName
objNewShare.MaxUserCount=2
objNewShare.SetInfo

'you may need to give the service a moment or two to finish this process
'before continuing
wscript.Sleep 5000

'we need to temporarily map a drive to the user's new directory so we
'can set permissions. Script will error and fail if the X: drive is
'already in use. You may want to add code to check if X: exists and
'disconnect it if it does.
wshNetwork.MapNetworkDrive "x:","\\"&strHomeSrv & "\" & strUserName & "$"

'set permissions
'windows are hidden. Change 0 to 1 to see windows
wshShell.Run "cmd /c echo y|cacls X:\ /g Administrators:F",0,True
wshShell.Run "cacls x:\ /e /g " & strDomain & "\" & strUserName & ":C",0,True

'drop network connection
wshNetwork.RemoveNetworkDrive "x:",True

'Force user to change password the first time they logon
set user=GetObject("WinNT://" & strDomain & "/" & strUserName & ",User")
User.Put "PasswordExpired",1
User.SetInfo

'Add user to a group or groups
'Repeat as needed.
dim objGroup
strGroup="Student"
set objGroup=GetObject("WinNT://" & strDomain & "/" & strGroup & ",Group")
objGroup.Add(user.ADSPath)
objGroup.SetInfo

set objGroup=Nothing
wscript.Echo "User account created for " & strUserName

'Empty our objects
set User=Nothing
set objUser=Nothing
set wshNetwork=Nothing
set objDS=Nothing
set objRoot=Nothing
set objDom=Nothing
set wshShell=Nothing

wscript.quit

'///////////////////////////
'/ Error Handling /
'///////////////////////////
Function ChkErr(errReturn,strMsg)
On Error Resume Next
If errReturn<>0 then
wscript.echo strMsg
set oDomain=Nothing
set oNewGroup=Nothing
set ochkGrp=Nothing
set oGrp=Nothing
wscript.quit
End If
End Function

Function ChkInput(strReturn,strMsg)
On Error Resume Next
If strReturn="" then
wscript.echo strMsg
set oDomain=Nothing
set oNewGroup=Nothing
set ochkGrp=Nothing
set oGrp=Nothing
wscript.quit
End If
End Function

'EOF

Jama
06-11-2002, 08:37 PM
I don’t know how much or how little data you have in your text file, and more importantly, what character are you using to separate the items in your file. So I have created a text file and a script that will dissect the data in the file and then display it in a message box.

Text File;

**************** Tab Separated Text File.txt ***************
<pre>
firstname lastname dateOfBirth department jobTitle
John Major 1962-11-24 accounting senior accountant
Tina Turner 1962-09-26 administration manager
Karen Doolan 1972-01-10 marketing graphic designer
Michael Azi 1978-02-11 research programmer
Sandra Bull 1976-10-26 marketing account manager
</pre>
************************************************** *****

Script;

'@@@@@@@@@@@@@ Parse a Fixed Width File.vbs @@@@@@@@@@@@

textFilePath = "C:\New Folder\Tab Separated Text File.txt"
Const ForReading = 1

Set FSo = CreateObject("Scripting.FileSystemObject")
Set ts = FSO.OpenTextFile(textFilePath, ForReading)


Do While Not Ts.AtEndOfStream
<font color=blue>
MyArray = split(ts.ReadLine,vbTab)
</font color=blue>
strFirstName = MyArray(0)
strLastName = MyArray(1)
strDateOfBirthName = MyArray(2)
strDepartment = MyArray(3)
strJobTitle = MyArray(4)
wscript.echo strFirstName & ", " & strLastName & ", " & strJobTitle

Loop

Ts.Close

Set ts = Nothing
Set Fso = Nothing
‘@@@@@@@@@@@@@@@@@@@@@@@@ eNd @@@@@@@@@@@@@@@@@@@@@@

The split function takes a string “in this case a line from the text file”, breaks it down to substrings and stores the data in a one- dimensional array called MyArray. You can then get any substring by referencing its array location. For example, to get the user name “the first element in the array” I’d use MyArray(0). This is because arrays are always 0 based. Bear this in mind because the count argument of the split function is not 0 based.

Apart from the count argument, the Split function has one mandatory argument and two optional ones. Try not to complicate things and use only the arguments you can’t do without.

The mandatory argument is the string to split. Um, you have to supply this!
The other argument you’ll need to use, is the Delimiter. This is the character that separates the data in your file. My text file is tab-separated so I used vbTab as the delimiter in my script. If your file is comma separated, change the line to;
MyArray = split(ts.ReadLine,”,”)

Once you get the hang of it, incorporate it in your main script to read the user details from the text file and create the accounts in AD.

Good luck

Jama

edpud
07-25-2002, 05:17 PM
i've got one that'll do it from a spreadsheet, i ran it when i set up a new w2k school network in surrey too!!!
reply if you want some help :-)

darkness
07-25-2002, 09:19 PM
I did it with our university I can send you a copy of our code just email me.

Michael McLaughlin/images/forums/icons/smile.gif
Systems Manager
Minnesota State University Mankato

loukes.philip.
09-10-2002, 05:18 PM
Can you sent me a copy as I have 10 schools I manage and I am in the process of setting up some new servers and need to add users. I have access to a excel list of students.
Thanks
Phil

Nailz
09-12-2002, 01:15 PM
Great! Excel makes it easy. Just save the file as ac Comma Separated Value. Then change the script to use the comma (,) as your delimiter for the split.

Leave it to Jama to tie it all together. :)

Dom
11-04-2002, 03:35 PM
Hello I just read your email and was wondering If I could get a copy of the code.

Cheers