Solved

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

Posted on 2006-11-24
5
151 Views
Last Modified: 2010-04-23
0
Comment
Question by:Montoya
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

861 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