imranasif17
asked on
VBScript to update Employee ID in the Active Directory
Hello Experts:
I have this VBScript that will read the comma delimited text file and update the active directory with Employee in the Employee ID field. My script first looks for the username in the text file and look for the same object in the active directory and if there is a match, then in that match look for the first name then last name and if there is a match again then update the employee id for that object.
What I want is could any expert modify this script such that if the Employee ID already exists then the script has to compare that with the value in the text data file and if Employee ID in the field and the data file are different then give a error message and if the employee ids match then also give a error message. The script must not modify the existing Employee ID in the Employee ID field.
The script must insert Employee ID in the Employee ID field only if the field is EMPTY.
Hope that I am clear.
Thanks.
I have this VBScript that will read the comma delimited text file and update the active directory with Employee in the Employee ID field. My script first looks for the username in the text file and look for the same object in the active directory and if there is a match, then in that match look for the first name then last name and if there is a match again then update the employee id for that object.
What I want is could any expert modify this script such that if the Employee ID already exists then the script has to compare that with the value in the text data file and if Employee ID in the field and the data file are different then give a error message and if the employee ids match then also give a error message. The script must not modify the existing Employee ID in the Employee ID field.
The script must insert Employee ID in the Employee ID field only if the field is EMPTY.
Hope that I am clear.
Thanks.
Option Explicit
Const ForReading = 1
Const ForAppending = 8
Const ADS_SCOPE_SUBTREE = 2
Dim WshShell
Dim WshSysEnv
Dim objFSO
Dim WshNetwork
Dim objConnection
Dim objCommand
Dim objRecordSet
Dim objTextFile
Dim objLogFile
Dim objUser
Dim strDN
Dim strDomain
Dim strLogFile
Dim TextFile
Dim strLine
Dim aUsers
Dim i
Dim strMsg
strDomain = "dc=LACCD,dc=TEST"
TextFile = "C:\work.csv"
strLogFile = "C:\logerror.log"
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objTextFile = objFSO.OpenTextFile(TextFile, ForReading)
Set objLogFile = objFSO.OpenTextFile(strLogFile,ForAppending,True)
objLogFile.WriteLine(Now & ": UpdateEmpID.vbs started")
'----------------------------------------------------------------
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
'-----------------------------------------------------------------
'This section deals with the CSV file
Do While Not objTextFile.AtEndOfStream
strLine = Trim(objTextFile.ReadLine)
If inStr(strLine, ",") Then
aUsers = Split(strLine, ",")
For i = 0 To UBound(aUsers)
aUsers(i) = Trim(aUsers(i))
Next
'Wscript.Echo ""
'Wscript.Echo "First Name...: " & aUsers(0)
'Wscript.Echo "Last Name....: " & aUsers(1)
'Wscript.Echo "UserName.....: " & aUsers(2)
'Wscript.Echo "Employee ID..: " & aUsers(3)
'This section deals with searching AD for the user account
'It takes the username (sAMAccountName), AKA the fourth field and actually searches AD for that user account.
'Then pulls the users Distinguished Name out of that account once it finds it.
'Then it uses that DN to bind to the account and set the description field.
On Error Resume Next
objCommand.CommandText = _
"SELECT distinguishedName, givenName, sn FROM 'LDAP://" & strDomain & "'" & _
" WHERE objectCategory = 'user' " & _
" AND sAMAccountName = '" & aUsers(2) & "'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
strDN = "?????"
Do Until objRecordSet.EOF
If UCase(Trim(aUsers(0))) = UCase(Trim(objRecordSet.Fields("givenName").Value)) _
And UCase(Trim(aUsers(1))) = UCase(Trim(objRecordSet.FIelds("sn").Value)) Then
strDN = objRecordSet.Fields("distinguishedName").Value
End If
objRecordSet.MoveNext
Loop
On Error Goto 0
'This section takes the info provided from the AD search above to attach to the user and set the description
If strDN = "?????" Then
strMsg = "User not found or names don't match for Username = " & aUsers(2)
WScript.Echo strMsg
objLogFile.WriteLine(strMsg)
Else
Set objUser = GetObject("LDAP://" & strDN)
'strMsg = "Script connects to AD"
'WScript.Echo strMsg
WScript.Echo objUser.Name & "'s account updated in AD"
On Error Resume Next
objUser.Put "EmployeeID", aUsers(3)
strMsg = "Employee Id inserted for: " & aUsers(2)
objLogFile.WriteLine(strMsg)
objUser.SetInfo
If Err.Number <> 0 Then
strMsg = "Failed to update EmployeeID for Username: " & aUsers(2)
WScript.Echo strMsg
objLogFile.WriteLine(strMsg)
Err.Clear
End If
On Error GoTo 0
End If
End If
Loop
objLogFile.WriteLine(Now & ": UpdateEmpID.vbs stopped")
objLogFile.Close
objTextFile.Close
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot. Solution in one shot.
No problem. Thanks for the grade.
Rob.
Rob.
Regards,
Rob.
Open in new window