Pages

Monday, October 12, 2009

Create Users in Active Directory

Script 1:
' This VBScript code creates a large number of users with incremented user names
' e.g. User1, User2, User3, ....
' ---------------------------------------------------------------
' From the book "Active Directory Cookbook" by Robbie Allen
' ISBN: 0-596-00466-4
' ---------------------------------------------------------------
' ------ SCRIPT CONFIGURATION ------

   1: intNumUsers = 10         ' Number of users to create
   2: strParentDN="CN=Users,dc=drkw,dc=tst" 'e.g.ou=bulk,dc=emea,dc=rallencorp,dc=com
   3: ' ------ END CONFIGURATION --------- 
   4:  
   5: ' Taken from ADS_USER_FLAG_ENUM
   6: Const ADS_UF_NORMAL_ACCOUNT = 512 
   7:  
   8: set objParent = GetObject("LDAP://" & strParentDN)
   9: for i = 11 to intNumUsers
  10:    strUser = "User" & i
  11:    Set objUser = objParent.Create("user", "cn=" & strUser)
  12:    objUser.Put "sAMAccountName", strUser 
  13:  
  14: ' CORRECTION: If you don't set userAccountControl, then by default
  15: '             the value of 514 (normal account + disabled) will be set for it.
  16: '             In this instance by setting it to 512, the account will not
  17: '             be disabled, and if you have password complexity enabled in
  18: '             your forest, the script will fail because a password was not
  19: '             set prior to the account being enabled.  The solution is to 
  20: '             not set userAccountControl here.
  21: '   objUser.Put "userAccountControl", ADS_UF_NORMAL_ACCOUNT 
  22:  
  23:    objUser.SetInfo
  24:    objUser.SetPassword("Aditi01*")
  25:    objUser.AccountDisabled=FALSE
  26:    objUser.SetInfo
  27:    'WScript.Echo "Created " & strUser
  28: next
  29: WScript.Echo ""
  30: WScript.Echo "Created " & intNumUsers & " users"



Script 2:




   1: ' UserSpreadsheet .vbs 
   2: ' Sample VBScript to create User accounts from a spreadsheet 
   3: ' 
   4: ' ------------------------------------------------------' 
   5: Option Explicit 
   6: Dim objRootLDAP, objContainer, objUser, objShell,objOU,objRootDSE,strDNSDomain
   7: Dim objExcel, objSpread, intRow ,Strmail,strNewGp
   8: Dim strUser, strOU, strSheet ,objGroup,strNewGpLong
   9: Dim strCN, strSam, strFirst, strLast, strPWD, strdisplay, strdesc, strprinc, strinit,strDN
  10: ' -------------------------------------------------------------' 
  11: ' Important change OU= and strSheet to reflect your domain 
  12: ' -------------------------------------------------------------' 
  13: strOU = "OU=Accounts ," ' Note the comma 
  14: strSheet = "C:\PerfAD1.xlsx" 
  15: ' Bind to Active Directory, Users container. 
  16: Const ADS_PROPERTY_APPEND = 3 
  17:  
  18: Set objRootLDAP = GetObject("LDAP://rootDSE") 
  19: Set objContainer = GetObject("LDAP://cn=Users," & _
  20:     objRootLDAP.Get("defaultNamingContext"))
  21: ' Open the Excel spreadsheet 
  22: Set objExcel = CreateObject("Excel.Application") 
  23: Set objSpread = objExcel.Workbooks.Open(strSheet) 
  24: intRow = 2 'Row 1 often contains headings 
  25: ' Here is the 'DO...Loop' that cycles through the cells 
  26: ' Note intRow, x must correspond to the column in strSheet 
  27:  
  28: 'Set objGroup = objContainer.Create("Group", "cn=PerfFeb10")
  29: 'objGroup.Put "sAMAccountName","GroupPerf30"
  30: 'objGroup.SetInfo 
  31: 'msgbox ("For Group Done...")
  32:  
  33: Do Until objExcel.Cells(intRow,1).Value = "" 
  34:   strSam = Trim(objExcel.Cells(intRow, 1).Value) 
  35:    'msgbox(strSam)
  36:   strCN = Trim(objExcel.Cells(intRow, 2).Value) 
  37:    'msgbox(strCN)
  38:   strFirst = Trim(objExcel.Cells(intRow, 3).Value) 
  39:    'msgbox(strFirst)
  40:   strLast = Trim(objExcel.Cells(intRow, 4).Value) 
  41:   strPWD = Trim(objExcel.Cells(intRow, 5).Value) 
  42:   strdisplay = Trim(objExcel.Cells(intRow, 6).Value) 
  43:   strdesc = Trim(objExcel.Cells(intRow, 7).Value) 
  44:   strprinc = Trim(objExcel.Cells(intRow, 8).Value) 
  45:   strinit = Trim(objExcel.Cells(intRow, 9).Value) 
  46:   Strmail= Trim(objExcel.cells(intRow, 10).Value)
  47:  'msgbox(Strmail)
  48:   ' Build the actual User from data in strSheet. 
  49:   Set objUser = objContainer.Create("User", "cn=" & strCN) 
  50:   objUser.put "sAMAccountName",strSam
  51:   objUser.put "givenName",strFirst
  52:   objUser.put "sn",strLast
  53:   objUser.put "displayName",strdisplay
  54:   objUser.put "description",strdesc
  55:   objUser.put "userPrincipalName",strprinc
  56:   objUser.put "mail",Strmail
  57:   objUser.put "initials",strinit
  58:    objUser.SetInfo 
  59:   ' Separate section to enable account with its password 
  60:    objUser.userAccountControl = 512 
  61:    objUser.pwdLastSet = 0 
  62:    objUser.SetPassword strPWD 
  63:    objUser.SetInfo 
  64:  
  65:  'strDN = ",cn=Users," & objRootLDAP.defaultNamingContext
  66:   'objGroup.PutEx ADS_PROPERTY_APPEND, "member",_
  67:     'Array("cn=" & StrCN & strDN)
  68:  
  69: '   objGroup.SetInfo
  70:  
  71:  
  72: intRow = intRow + 1 
  73: 'msgbox ("finished")
  74: 'msgbox("done")
  75: Loop 
  76: msgbox("done")
  77: objExcel.Quit 
  78: WScript.Quit 
  79:  
  80: ' End of Sample UserSpreadsheet VBScript. 
  81:  
  82:  
  83:  

Excel:-

AccountName
username
First
Last
pwd
display
description
Principal
initials
Mails
Luser1
Luser1
Luser1
Luser1
password
Luser1
dev
addd
a
venky1018@gmail.com
Luser2
Luser2
Luser2
Luser2
password
Luser2
dev
addd
a
venky1018@gmail.com










0 comments: