Search This Blog

Sunday, 8 May 2011

Adding bulk of users into AD (active directory) from CSV file vbscript

Adding bulk of users into AD (active directory) from CSV file. It’s best to execute these scripts from a domain controller while logged in with administrative credentials. I’m assuming that users details you can get a comma-separate value (CSV) . Save CSV file on correct location example "C:\vbScripts\".

CSV file example:

Vbscript:


Option Explicit

Dim sCSVFileLocation
Dim sCSVFile
Dim objFSO
Dim objFile
Dim strLine
Dim strItems
Dim oNewUser

' ———-LDAP connection variables———-
Dim oRootLDAP
Dim oContainer
Dim objRootLDAPg, objGroup, objUser, objOU
Dim strOU, strGroup, strDNSDomain
Dim intCounter

' ———-Other variables——————–
Dim sGivenName
Dim sInitials
Dim sSurName
Dim sDescription
Dim sDepartment
Dim nPwdLastSet
Dim nUserAccountControl
Dim sDomain

' ———-Modify this to match your company’s AD domain———-
sDomain="firemonkey.net"

' ———-Input file location———-
sCSVFileLocation = "C:\ScriptS\"
' ———-Full path to input file———-
sCSVFile = sCSVFileLocation&"test.csv"

'Wscript.Echo sCSVFile

' ———-Commands used to open the CSV file and select all of the records———-
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(sCSVFile, 1)

' ———-Create a connection to the Active Directory Users container.———-

Set oContainer = GetObject("LDAP://OU=Test,dc=firemonkey,dc=net")

' ———-Allows processing to continue even if an error occurs (i.e. dup user)———-
on error resume next

Do Until objFile.AtEndOfStream ' Reads the values (cells) in the sInputFile file.

' ——— Start creating user account———-
' Read variable information from the CSV file
' and build everything needed to create the account
strLine = objFile.ReadLine
strItems = split(strLine,",")

sGivenName = strItems(0)
sInitials = strItems(1)
sSurName = strItems(2)
sDescription = strItems(3)
sDepartment = strItems(4)
nPwdLastSet = strItems(5)
nUserAccountControl = strItems(6)

' ———-Build the User account———-
Set oNewUser = oContainer.Create("User","cn="&sGivenName)

oNewUser.put "givenName",sGivenName
oNewUser.put "initials",sInitials
oNewUser.put "sn",sSurName
oNewUser.put "Description",sDescription
oNewUser.put "Department",sDepartment

' ———-Write this information into Active Directory so we can———-
' modify the password and enable the user account
oNewUser.SetInfo

' ———-Change the users password———-
oNewUser.Put "pwdLastSet",nPwdLastSet

' ———-Enable the user account———-
oNewUser.Put "userAccountControl", nUserAccountControl
oNewUser.SetInfo

Loop
objFile.Close
Wscript.Echo "All accounts are successfully copied!!!"

' ——— End of user account creation———-




' ——— Add users into students group———-

strOU = "OU=Group2,"
strGroup = "CN=Students,"

' Bind to Active Directory and get LDAP name
Set objRootLDAP = GetObject("LDAP://RootDSE")
strDNSDomain = objRootLDAPg.Get("DefaultNamingContext")

' Prepare the OU and the Group
Set objGroup = GetObject("LDAP://"& strGroup & strOU & strDNSDomain)
Set objOU =GetObject("LDAP://" & strOU & strDNSDomain)

' On Error Resume next
intCounter = 1
For Each objUser In objOU
   If objUser.Class = lcase("User") then
      objGroup.add(objUser.ADsPath)
      intCounter = intcounter +1
   End If
Next
WScript.Echo strGroup & " has " & intCounter & " new members"

Wscript.Quit

No comments:

Post a Comment