Question : Lookup Email Addresses from large text file list and verify if exists in AD

Hello Scripters,

I need your assistance. I have a txt file (I can reformat however needed) of thousands of email addresses. I need to query Active Directory using this file as input and then get an output file that will indicate if these email addresses exist in Active Directory or not. It needs to lookup these addresses to see if they are primary or proxy addresses as well.

Thanks for your help!

Answer : Lookup Email Addresses from large text file list and verify if exists in AD

Give this a try.  The input file should have one email address per line.

The output file shows the email address and if it was found or not found

This will query primary and alias email addresses.

Let me know how it works for you.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
Const ForReading = 1
Const ADS_SCOPE_SUBTREE = 2
 
strInput = "c:\scripts\addresses.txt"
strOutput = "c:\scripts\output.txt"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objInputFile = objFSO.OpenTextFile(strInput, ForReading)
 
strText = objInputFile.ReadAll
objInputFile.Close
 
Set objOutputFile = objFSO.CreateTextFile(strOutput)
 
Set objRoot = GetObject("LDAP://RootDSE")
strDNSDomain = "LDAP://" & objRoot.Get("defaultNamingContext")
Set adoConnection = CreateObject("ADODB.Connection")
Set adoCommand = CreateObject("ADODB.Command")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection
adoCommand.Properties("Page Size") = 1000
adoCommand.Properties("Timeout") = 300
adoCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
 
arrAddresses = Split(strText, vbCrLf)
 
For Each strAddress In arrAddresses
	strAddress = Trim(strAddress)
	If strAddress <> "" Then
		adoCommand.CommandText = "SELECT ADsPath FROM '" & strDNSDomain & _
		"' WHERE mail='" & strAddress & "' OR proxyAddresses='*" & _
		strAddress & "*'"
		Set adoRecordset = adoCommand.Execute
		
		If adoRecordset.EOF Then
			objOutputFile.WriteLine strAddress & VbTab & "Not Found"
		End If
		
		Do Until adoRecordset.EOF
			objOutputFile.WriteLine strAddress & VbTab & "Found"
			adoRecordset.MoveNext
		Loop
	End If
Next
Random Solutions  
 
programming4us programming4us