Solved

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

Posted on 2006-11-24
5
148 Views
Last Modified: 2010-04-23
0
Comment
Question by:Iammontoya
  • 3
5 Comments
 
LVL 1

Expert Comment

by:wpgatrel
ID: 18009680
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
0
 
LVL 1

Accepted Solution

by:
wpgatrel earned 20 total points
ID: 18009692
Doh!  I meant to paste in THIS code.

'Script Start
Set objFSO = CreateObject("scripting.FileSystemObject")

LogFile="ContactInfo.CSV"

Set objRootDSE = GetObject("LDAP://RootDSE")
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 samAccountName, GivenName, SN, telephonenumber, mail 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
      szMail = objRecordSet.Fields("Mail").Value
      szTelephoneNumber = objRecordSet.Fields("TelephoneNumber").value

      f.writeline szGivenName & "," & szSN & "," & szSamAccountName & "," & szMail & "," & sztelephoneNumber

      
objRecordSet.MoveNext
Loop

f.close

wscript.echo "Script Complete!"
'Script End
0
 
LVL 1

Expert Comment

by:wpgatrel
ID: 18152438
Did it get you what you needed?
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now