Re: scripting domain users accounts by Risrollout
Risrollout
Fri Nov 05 17:38:51 CST 2004
Here is a simple script that will take infomation from a CSV file. It will
add the users to a nested ou and a security group. You will need to create
the OUs and groups ahead of time. If you Google "Create user in AD" you will
get a lot of sites to get ideas from to adjust this script to your needs.
Set up your csv file like this.
Firstname,lastname,password, ou,ou,security group
Our company uses 5/3 naming convention so you will have to change that to
match yours.
Option Explicit
On Error Resume Next
Dim aLine, sLine, sCN, sLogon, sPass, sOU, sOUb, sOUp, sGroup
Dim oTF, oFSO, oOU, oUser, oGroup
Const ForReading = 1
'// Bind to the filesystem object
Set oFSO = CreateObject("Scripting.FileSystemObject")
'// Open up the csv file. Make sure the path here is correct
Set oTF = oFSO.OpenTextFile("\\server\AddUsers.csv",ForReading,True)
'// Run a Loop to read all the lines in the file
Do While oTF.AtEndOfStream <> True
'// Read a line of the file
sLine = oTF.ReadLine
'// Create an array split on commas
aLine = split(sline, ",",-1,1)
'// login is first 5 of FN and first 3 LN
sLogon = Left(aLine(0), 5) & Left(aLine(1), 3)
'// display name is FN LN
sCN = aline(0) & " " & aLine(1)
sPass = aLine(2)
sOU = aLine(3)
sOUb = aline(4)
sOUp = aline(5)
sGroup = aline(6)
sLogonScript = start.bat
'// Call the Create User routine
CreateaUser
'// Call the add to Group routine
Add2Group
Loop
Msgbox "Script end.",vbinformation, "Add Users"
Set oTF = Nothing
Set oFSO = Nothing
Set oUser = Nothing
Set oGroup = Nothing
Set oOU = Nothing
Set sFN = Nothing
Set sLN = Nothing
Set sLogon = Nothing
Set sCN = Nothing
Set sPass = Nothing
Set sOU = Nothing
Set sOUb = Nothing
Set sOUp = Nothing
Set sGroup = Nothing
' Get domain name.
set objRoot = GetObject("LDAP: //RootDSE")
strDomainCtl = objRoot.Get("defaultNamingContext")
Sub CreateaUser()
Set oOU = GetObject ("LDAP://ou=" & sOU & ",ou=" & sOUb & ",ou=" & sOUp &
"," & strDomainCtl)
Set oUser = oOU.Create("user", "cn=" & sCN)
oUser.put "givenName", aLine(0)
oUser.put "sn", aLine(1)
oUser.put "sAMAccountName", lcase(sLogon)
oUser.put "userPrincipalName", lcase(sLogon) & "@yourdomain.com"
oUser.put "DisplayName", sCN
oUser.put "scriptPath", sLogscript
'// Need to commit this to AD before adding the password
oUser.SetInfo
If err.number = -2147019886 then
msgbox "User logon for " & sLogon & " already exists." ,vbExclamation, "Add
Bulk Users"
Exit Sub
End If
oUser.SetPassword sPass
oUser.AccountDisabled = False
oUser.SetInfo
End Sub
Sub Add2Group()
Const ADS_PROPERTY_APPEND = 3
Set oGroup = GetObject ("LDAP://cn=" & sGroup & ",ou=Security Groups," &
strDomainCtl)
oGroup.PutEx ADS_PROPERTY_APPEND, "member", Array("cn=" & sCN & ",ou=" & sOU
& ",ou=" & sOUb & ",ou=" & sOUp & "," & strDomainCtl)
oGroup.SetInfo
End Sub
"Brian McHugh" <bjmchugh@insightbb.com> wrote in message
news:RxBid.364921$D%.19351@attbi_s51...
>i am about to roll out a a new AD Structure. i need a script that will add
>about 2000 user accounts. i obviously do not want to type all of them out.
>is there a script that i can run that will call a text file with all the
>user information in it?