Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript Exchange only returns up to 'R'

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hi,

Knowing which forum to place this one in is a little tricky so looking at similar posts, I have chosen this one. Appologies if you don't agree...

Anyway, I have a script that pulls out all email addresses into a csv file, but it is only returning users up to 'R'. The script doesn't error and I can't see any reason for it in AD, Exchange or the script itself. Would anybody be able to shed any light on this?

Here is the script:

Code:
Set rootDSE=GetObject("LDAP://RootDSE")
DomainContainer = rootDSE.Get("defaultNamingContext")
csvFile = "C:\e-mails.csv"
Set fso = CreateObject ("Scripting.FileSystemObject")
If fso.FileExists(csvFile) Then
	fso.DeleteFile(csvFile)
	Set emFile = fso.CreateTextFile ("c:\e-mails.csv")
Else 
	Set emFile = fso.CreateTextFile ("c:\e-mails.csv")
End If

Set connDb = CreateObject("ADODB.Connection")
connDb.Provider = "ADSDSOObject"
connDb.Open "ADs Provider"

ldapStr = "<LDAP://" & DomainContainer & ">;(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(!(homeMDB=*))(!(msExchHomeServerName=*)))(&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))) ));adspath;subtree"

Set rs = connDb.Execute(ldapStr)

While Not rs.EOF
	Set oUser = GetObject (rs.Fields(0).Value)
	emFile.Write oUser.displayName &  "," 
	      for each email in oUser.proxyAddresses  
	          If InStr(email,"@myChosenDomain.co.uk") Then
	          	emFile.Write email & ","
	      	  End If
	      Next 
	        emFile.WriteLine ""
	      rs.MoveNext
Wend

Many thanks in advance.

Woter
 
Would the number of entries up to R be about 1000? Search on the web and you will find that there is an implicit limit on the number of records returned. The only way that I know to get around it is to use the ADODB.COmmand object and explicitly set it's PageSize property. Here is an example:

Set oRootDSE = GetObject("LDAP://rootDSE")
strDNC = oRootDSE.get("defaultNamingContext")
strPCQuery = "<LDAP://" & strDNC & ">;(objectCategory=computer)" & ";distinguishedName,name;subtree"
Set oConnection = CreateObject("ADODB.Connection")
Set oCommand = CreateObject("ADODB.Command")
oConnection.Open "Provider=ADsDSOObject;"
oCommand.ActiveConnection = oConnection
oCommand.CommandText = strPCQuery
oCommand.Properties("Page Size") = 100
Set oRecordSet = oCommand.Execute

This pulls the data for every computer in a domain.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Yes. That will be it, because the original CSV file has exactly 1000 entries. I didn't notice this as I started deleting unwanted address and sorting the data.

Many thanks

Woter
 
Thanks EBgreen.

For anyone who whats a script to pull / get all email addresses out of Microsoft Exchange / AD, here is my script. Yes there are other and better ways...

One can set 'applyFilter' to false. This turns off the filter that shows only one domains email.

Code:
' Settings - Change these as required
applyFilter = True 'Set to false if you want to return all email address for a user
				   'as opposed to just one domain.
filterDomain = "@familymosaic.co.uk"
csvFile = "C:\e-mails.csv" 'output file name and location

'End settings

Set rootDSE=GetObject("LDAP://RootDSE")
DomainContainer = rootDSE.Get("defaultNamingContext")

Set fso = CreateObject ("Scripting.FileSystemObject")
If fso.FileExists(csvFile) Then
	fso.DeleteFile(csvFile)
	Set emFile = fso.CreateTextFile ("c:\e-mails.csv")
Else 
	Set emFile = fso.CreateTextFile ("c:\e-mails.csv")
End If

Set oConn = CreateObject("ADODB.Connection")
Set oCmd = CreateObject("ADODB.Command")
oConn.Provider = "ADSDSOObject"
oConn.Open "ADs Provider"

ldapStr = "<LDAP://" & DomainContainer & ">;(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(!(homeMDB=*))(!(msExchHomeServerName=*)))(&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))) ));adspath;subtree"

oCmd.ActiveConnection = oConn
oCmd.CommandText = ldapStr
oCmd.Properties("Page Size") = 2000 'make sure this value is greater than the number of user entries in AD.

Set oRecordSet = oCmd.Execute
WScript.echo "Please wait... Gathering users' email addresses."
count = 0
While Not oRecordSet.EOF
	Set oUser = GetObject (oRecordSet.Fields(0).Value)
	emFile.Write oUser.displayName &  "," 
	      
	      for each email in oUser.proxyAddresses  
	          If applyFilter = True Then 
		          If InStr(email,filterDomain) Then 'use to filter 
		          	
		          	emFile.Write email & ","
		      	  	count = count +1
		      	  End If
		       Else
		       	 emFile.Write email & ","
		         count = count +1
		       End If
	      Next 
	      emFile.WriteLine ""
	      oRecordSet.MoveNext
Wend
WScript.Echo "Finished collecting email addresses. There are " & count & " addresses."

Many thanks

Woter
 
I have ammended the LDAP string to excluded disabled accounts. Basically it says ...and (don't)(!) show disabled accounts.

For example:
Code:
(!userAccountControl:1.2.840.113556.1.4.803:=2)

Here is the new code in ldapStr:
Code:
ldapStr = "<LDAP://" & DomainContainer & ">;(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)" &_
			"(!(homeMDB=*))(!(msExchHomeServerName=*)))(&(objectCategory=person)(!userAccountControl:1.2.840.113556.1.4.803:=2)"&_
			"(objectClass=user)(|(homeMDB=*)"& _
			"(msExchHomeServerName=*))) ));adspath;subtree"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top