Solved

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

Posted on 2006-11-24
5
154 Views
Last Modified: 2010-04-23
0
Comment
Question by:Montoya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

630 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