PC_Aide

2_create_users_montreal-ou (direction).vbs

Dec 18th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' CreateBulkADUsersFromCSVFile.vbs
  2. ' Sample VBScript to create a AD Users from CSV file .
  3. ' Author: http://www.morgantechspace.com/
  4. ' ------------------------------------------------------'
  5. Option Explicit
  6.  
  7. ' Variables needed for LDAP connection
  8. Dim objRootLDAP
  9. Dim objContainer
  10.  
  11. ' Variables needed for CSV File Information
  12. Dim varFileName
  13. Dim objFSO
  14. Dim objFile
  15.  
  16. ' Holding variables for user information import from CSV file
  17. Dim varSamAccountName,varFirstName,varLastName
  18. Dim newUserFields
  19.  
  20. Dim objNewUser
  21. Dim varDomain
  22.  
  23. Const ForReading = 1
  24.  
  25. ' Modify this name to match your company's AD domain
  26. varDomain="workdomain.local"
  27.  
  28. ' Create a connection to the Active Directory Users container.
  29. Set objRootLDAP = GetObject("LDAP://rootDSE")
  30.  
  31. ' You can give your own OU like LDAP://OU=TestOU instead of LDAP://cn=Users
  32. Set objContainer = GetObject("LDAP://ou=direction,ou=montreal-ou,ou=educ-plusinc," & objRootLDAP.Get("defaultNamingContext"))
  33.  
  34. ' Specify the csv file full path.
  35. varFileName = ".\2_list_Users_direction.csv"
  36.  
  37. ' Open the file for reading.
  38. Set objFSO = CreateObject("Scripting.FileSystemObject")
  39. Set objFile = objFSO.OpenTextFile(varFileName, ForReading)
  40.  
  41. ' Read the first line - csv columns -not needed for our proceess
  42. objFile.ReadLine
  43.  
  44. ' Skip the error while creating new user...(i.e- user already exists)
  45. on error resume next
  46.  
  47. ' Read the file and create new user.
  48. Do Until objFile.AtEndOfStream
  49.     ' Splits prioperty values.
  50.    newUserFields = Split(objFile.ReadLine,",")
  51.     varSamAccountName = newUserFields(0)
  52.     varFirstName = newUserFields(1)
  53.     varLastName = newUserFields(2)
  54.  
  55. ' Create new User account
  56. Set objNewUser = objContainer.Create("User","cn="&varFirstName&" "&varLastName)
  57.  
  58. objNewUser.put "sAMAccountName",lcase(varSamAccountName)
  59. objNewUser.put "givenName",varFirstName
  60. objNewUser.put "sn",varLastName
  61. objNewUser.put "UserPrincipalName",lcase(varSamAccountName)&"@"&varDomain
  62. objNewUser.put "DisplayName",varFirstName&" "&varLastName
  63. objNewUser.put "name",lcase(varSamAccountName)
  64. objNewUser.put "description","This user was created from csv file using vbscript"
  65.  
  66. objNewUser.SetInfo
  67. objNewUser.Put "pwdLastSet", 0
  68.  
  69. ' Enable the user account
  70. objNewUser.AccountDisabled = FALSE
  71. objNewUser.SetInfo
  72. Loop
  73.  
  74. MsgBox("Active Directory users created successfully from CSV file using VBScript.")
  75.  
  76. WScript.Quit
Advertisement
Add Comment
Please, Sign In to add comment