Tuesday, December 18, 2007

Scripts: Create a list of email addresses in use by the organization

I had a client ask for this the other day… while I had a few things that I probably could have used for a template, I decided to skim though the Microsoft Script Center repository, and found something close in their AD section. After checking the MSDN site, I found the attribute I was looking for, “mail”. Modified the script a bit – and it worked.

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

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

objCommand.CommandText = _
"SELECT givenName, sn, Mail, homeDirectory FROM 'LDAP://dc=domain,dc=local' WHERE objectclass='user' "
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strGivenName = givenName
Wscript.Echo "Name: " & objRecordSet.Fields("givenName").Value
Wscript.Echo "Last Name: " & objRecordSet.Fields("sn").Value
Wscript.Echo "EMail: " & objRecordSet.Fields("Mail").Value
Wscript.Echo "Home Drive: " & objRecordSet.Fields("homeDirectory").Value
Wscript.Echo " "
objRecordSet.MoveNext
Loop

No comments: