Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to add bulk users to domain

Status
Not open for further replies.

phonehelp

Technical User
Jul 3, 2002
84
US
I'm using this script to add users to a domain from an excel spreadsheet. Presently if a user is already present a msgbox pops up with the username which I have to acknowlegde for the script to continue. I need to modify the script to write duplicate user names to a log file and continue running .




'Run Script form c:\Temp\Til


'Option Explicit
Dim strHomeFolder, strHome, strUser,FirstName,LastName,Password,Groupid
Dim objFSO,objShell, intRunError
Dim objExcel, strSheet, objSpread, intRow, strOU, strOU1,strDNSDomain,nName




'set path for home directory
'strHome="C:\Home\TIL\"

'to change to tlcpenguin
strhome ="\\tlcpenguin\TIL\Home"


'Const ADS_PROPERTY_APPEND = 3
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")



'Read from Excel file

strSheet = "C:\Temp\TIL\names.xls"


'' Bind to Active Directory, Users container.
Set objRootLDAP = GetObject("LDAP://rootDSE")
strDNSDomain = objRootLDAP.Get("defaultNamingContext")

' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSpread = objExcel.Workbooks.Open(strSheet)
intRow = 3 'Row 1 often contains headings

' Here is the 'DO...Loop' that cycles through the cells
' Note intRow, x must correspond to the column in strSheet

Do Until objExcel.Cells(intRow,1).Value = ""

Firstname = Trim(objExcel.Cells(intRow, 1).Value)
Lastname = Trim(objExcel.Cells(intRow, 2).Value)
Password = Trim(objExcel.Cells(intRow, 3).Value)
Groupid = Trim(objExcel.Cells(intRow, 4).Value)


on error resume next
userName = Left(Firstname,1) & Lastname
nUser=userName
FullName = FirstName & " " & LastName

Set objContainer = GetObject("LDAP:// OU=NE,OU=TIL,dc=homenet,dc=com")

'Change domainname

'create user account
Set objUser = objContainer.Create("user", "cn=" & fullname)
objUser.Put "sAMAccountName", username
objuser.sn = LastName
objuser.givenname = FirstName
objuser.displayname = FirstName & " " & LastName
objuser.userPrincipalName = UserName & "@homenet.com"
'objuser.Description = "TIL EMP"
objuser.SetInfo

' Used only for debugging
if err.number = -2147019886 then
msgbox "User logon = " & username & " already exists"
End If

'Add user to group

strGroup= "CN="& Groupid
StrOU =",OU=NE,"
StrOU1= "OU=TIL,"
strUser ="CN=" & FullName



Const ADS_PROPERTY_APPEND = 3

'WScript.Echo StrUser, strGroup, StrOU, strOU1 , strDNSDomain

Set objUser = GetObject("LDAP://"& strUser & strOU & strOU1 & strDNSDomain)

Set objGroup = GetObject ("LDAP://" & strGroup & strOU & strOU1 & strDNSDomain)
objGroup.add(objUser.ADsPath)

'WScript.Echo "Check " & strOU & " for " & strGroup & " = " & strUser



'Adding passwords form Excel File
objuser.SetPassword Password
objuser.AccountDisabled = FALSE
objUser.Put "pwdLastSet", 0
objuser.SetInfo

'Create home Dir
strHomeFolder = strHome &nUser
objFSO.CreateFolder strHomeFolder
intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls "_
& strHomeFolder & " /t /c /g Administrators:f "_
& strUser & ":F", 2, True)



intRow = intRow + 1
Loop
objExcel.Quit

WScript.Quit



 
You don't know how to replace this ?
msgbox "User logon = " & username & " already exists"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes, how do I replace this to write to a log file.
Thanks
 
After you create a user object and define it's properties, conditionalize the rest. You're half way there

Code:
Do Until objExcel.Cells(intRow,1).Value = ""
   'Set variables from Excel File
   'Change domain
   'Create user object
   if err.number = -2147019886 then
      msgbox "User logon = " &  username  &  "       already exists"
   [red]else[/red]
      'Add user to group
      'Adding passwords from Excel file
      'Create home dir.
   [red]end if[/red]
   intRow = intRow + 1
   set objUser = nothing
Loop

objExcel.Quit
WScript.Quit

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top