1. Option Explicit
  2. Const ForAppending = 8
  3. Const ForReading = 1
  4. Const ForWriting = 2
  5.  
  6. Dim objOutput, objInput, fso, adoConnection, objRoot, objShell, strDomain, strBase, strLine, arrLine, objDomain, strFilter, strAttribs, strDepth, strQuery, adoRecordSet
  7. Dim bolFound, strGiven
  8.  
  9. Set fso = CreateObject("Scripting.FileSystemObject")
  10. Set objInput = fso.OpenTextFile("C:\scripts\input.csv", ForReading)
  11. Set objOutput = fso.OpenTextFile("C:\scripts\output.txt", ForWriting, True)
  12. Set adoConnection = CreateObject("ADODB.Connection")
  13. Set objRoot = GetObject("LDAP://rootDSE")
  14. Set objShell = CreateObject("WScript.Shell")
  15. strDomain = objRoot.Get("defaultNamingContext")
  16. Set objDomain = GetObject("LDAP://" & strDomain)
  17. strBase = "<" & objDomain.ADsPath & ">"
  18.  
  19. adoConnection.Open "Data Source=Active Directory Provider;Provider=ADsDSOObject"  
  20.  
  21. Do Until objInput.AtEndOfStream
  22.     strLine = objInput.ReadLine
  23.     arrLine = Split(strLine,",")
  24.     'wscript.echo arrLine(0) & " " & arrLine(1)
  25.     strFilter = "(&(objectCategory=person)(objectClass=user)(sn=" & arrLine(0) & "))"
  26.     strAttribs = "sAMAccountName,givenName"
  27.     strDepth = "subTree"
  28. '  
  29. '   'Search the directory for the account name by last name
  30.     strQuery = strBase & ";" & strFilter & ";" & strAttribs & ";" & strDepth
  31.    
  32.     Set adoRecordSet = adoConnection.Execute(strQuery)
  33.     If adoRecordSet.recordCount = 0 Then
  34.         wscript.echo arrLine(1) & " " & arrLine(0) & vbTab & "No users found."
  35.     Else
  36.         bolFound = 0
  37.         While Not adoRecordSet.EOF
  38.             strGiven = adoRecordSet.Fields("givenName").Value
  39.             If strComp(strGiven,arrLine(1)) = 0 Then
  40.                 bolFound = 1
  41.                 objOutput.writeline adoRecordSet.Fields("sAMAccountName").Value
  42.             End If
  43.             adoRecordSet.MoveNext
  44.         Wend
  45.         if bolFound = 0 Then
  46.             wscript.echo arrLine(1) & " " & arrLine(0) & " Could not match." & vbTab &_
  47.             "found: " & strGiven & vbTab & "input:"  & arrLine(1)
  48.            
  49.         End If
  50.     End If
  51. Loop
  52.  
  53.  
  54.  
  55. Set objOutput = Nothing
  56. Set objInput = Nothing
  57. Set fso = Nothing