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
'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