Link to home
Start Free TrialLog in
Avatar of Montoya
Montoya

asked on

Still looking for help. Very Urgent. Extract contact information from AD and print

Avatar of wpgatrel
wpgatrel

Here is a simple VBScript that will read ALL user accounts in Active Directory and place the following items into a CSV file.  It will place the CSV file in the same location of the script.  You can use Excel to manipulate it and print it the way you want.

First Name, Last Name, Network Login, E-Mail Address, Telephone Number

'Script Start
Const ADS_SCOPE_SUBTREE = 2
CONST ForReading = 1, ForWriting = 2, ForAppending = 8

Dim DomainName, ParentDir, Server, Lengthy, Lengthy2, Drive, szGivenName, szSN
Dim Command1, WSHNetwork, FS, Domain, DomainObj, Computer, ShareServiceObj, Hidden, DrivePath, nReturnCode

Set objFSO = CreateObject("scripting.FileSystemObject")

LogFile="ContactInfo.CSV"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

strDNSDomain = objRootDSE.Get("DefaultNamingContext")

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select * from 'LDAP://" & strDNSDomain & "' Where objectClass='User'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

set f = objFSO.OpenTextFile(LogFile, ForWriting, True, -2)

f.writeline "First Name,Last Name,Network Login,Email Address,Telephone"

Do Until objRecordSet.EOF


      szSamAccountName = objRecordSet.Fields("samAccountName").Value
      szGivenName = objRecordSet.Fields("GivenName").Value
      szSN = objRecordSet.Fields("SN").Value
      szCN = "CN=" & szGivenName & " " & szSN

      f.writeline szGivenName & "," & szSN & "," & szSamAccountName & "," & Email & "," & telephone

      
objRecordSet.MoveNext
Loop

f.close

wscript.echo "Script Complete!"
'Script Complete
ASKER CERTIFIED SOLUTION
Avatar of wpgatrel
wpgatrel

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
Did it get you what you needed?