Link to home
Start Free TrialLog in
Avatar of imranasif17
imranasif17Flag for United States of America

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

Open in new window

Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, try this script. This will only attempt to update if the EmployeeID is emptry, and will log a message if they are different, or if they match.

Regards,

Rob.
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
Dim strEmpID
 
'strDomain  = "dc=LACCD,dc=TEST"
'TextFile   = "C:\work.csv"
'strLogFile = "C:\logerror.log"
 
strDomain = "DC=Maroondah,DC=local"
TextFile = "Users.csv"
strLogFile = "Users_Log.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
			strEmpID = objUser.EmployeeID
			If strEmpID = "" Then
				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
			ElseIf strEmpID <> aUsers(3) Then
				strMsg = "EmployeeID difference for Username: " & aUsers(2) & " in AD: " & strEmpID & " in CSV: " & aUsers(3)
				WScript.Echo strMsg
				objLogFile.WriteLine(strMsg)
			ElseIf strEmpID = aUsers(3) Then
				strMsg = "EmployeeID match for Username: " & aUsers(2) & " in AD: " & strEmpID & " in CSV: " & aUsers(3)
				WScript.Echo strMsg
				objLogFile.WriteLine(strMsg)				
			End If
			On Error GoTo 0			
		End If
	End If
Loop
objLogFile.WriteLine(Now & ": UpdateEmpID.vbs stopped")
objLogFile.Close
objTextFile.Close

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of imranasif17

ASKER

Thanks a lot.  Solution in one shot.
No problem. Thanks for the grade.

Rob.