Link to home
Start Free TrialLog in
Avatar of Montoya
Montoya

asked on

Get phone list from AD and print nicely using PDF or other method

Basically speaking, I need to use VB.Net to read AD account information, extract it, and hand off to a nicely printed solution, like an access report, a pdf document or anything else that would look nice. For a bonus 1000 points, they would like to choose which fields show up on the report, but that's just a bonus. Any ideas would be greatly appreciated.

Avatar of newyuppie
newyuppie
Flag of Ecuador image

what is AD?
Avatar of Montoya
Montoya

ASKER

Active Directory, which is where I'd have to get the users' mailbox information after Exchange 5.5

i believe you would use the System.DirectoryServices to directly access functions that will let you retrieve that info.  here are a couple of links i googled up to get you started:

http://www.vbdotnetheaven.com/UploadFile/johncharles/ActiveDirectoryInVB11122005060642AM/ActiveDirectoryInVB.aspx?ArticleID=a775db4b-3909-4d36-86e2-917b6d693465
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdirectoryservices.asp

when you have the info, to make reports you basically have 2 options: built-in vb net reporting, or third party. vb2005 comes with limited crystal reports reporting. you can also buy tools like XtraReports from devexpress.com to make nice, user customizable reports.

hope this info helps you.
NY
Avatar of Montoya

ASKER

I was hoping for more guided directions or code, please, if anyone can help with this.
were you hoping we do the work for you too?

read this codeproject link it may suit you:
http://www.codeproject.com/dotnet/activedirquery.asp

i also googled up this: http://www.15seconds.com/Issue/060525.htm
Avatar of Montoya

ASKER

no, I wasnt hoping you would do the work for me. I was hoping for some guidance, as stated above, or at least the same kind of help I try to provide as a guru in another section in this site, or the kind of help I would expect as a paying customer of this site.

Thanks for the links.

hey im sorry, for personal reasons i was in a very bad mood that day.
wish i could help you more with the issue but i dont know much myself. hope you can make something out from those links
NY
Avatar of Montoya

ASKER

No problem. We all have our days. :)


If someone else has some insight to this issue, I'd greatly appreciated.
Hi Iammontoya,

Here is a sample that achieves what you want (in principle). You could easily extend this, at present it returns a datatable of people from activedirectory matching part of the cn property.

Imports System.DirectoryServices

....... standard stuff, class etc

    Private Sub GetPhoneBook()
        Dim strMatch As String = "John"
        Dim dtAD As New DataTable
        dtAD.TableName = "Default"
        dtAD.Columns.Add("UserName")
        dtAD.Columns.Add("Email Address")
        dtAD.Columns.Add("Telephone")
        dtAD.Columns.Add("Mobile")
        dtAD.Columns.Add("Fax")
        dtAD.Columns.Add("Title")
        dtAD.Columns.Add("Description")
        dtAD.Columns.Add("Facility")
        dtAD.Columns.Add("Address")
        dtAD.Columns.Add("Town")
        dtAD.Columns.Add("PostCode")
        If Not (strMatch Is Nothing) Then
            GetAD("corp.mydomain.com", dtAD, strMatch) 'specify the ad domain to search here, if multiple domains, can call GetAD() succesive times to return data to the same datatable
            dtAD.DefaultView.Sort = "UserName"
'here we do something with the data, in this case bind it to a datagrid, anything else is up to you.
            grdPeople.DataSource = dtAD
            grdPeople.DataBind()
        End If
    End Sub

    Private Sub GetAD(ByVal Root As String, ByRef dtAD As DataTable, ByVal Match As String)
        Dim strLDAP As String = "LDAP://" & Root
        Dim obDirEntry As DirectoryEntry = New DirectoryEntry(strLDAP)
        Dim srch As DirectorySearcher = New DirectorySearcher(obDirEntry)
        Dim results As SearchResultCollection
        srch.Filter = "(cn=*" & Match & "*)"
        Try
            results = srch.FindAll()
        Catch ex As Exception
            Exit Sub
        End Try
        Dim rstl As SearchResult
        For Each rstl In results
            Dim colProps As System.DirectoryServices.ResultPropertyCollection = rstl.Properties
            Dim aryElements(10) As String
            aryElements(0) = GetElement("displayname", colProps)
            aryElements(1) = "<a href=""mailto:" & GetElement("mail", colProps) & """>" & GetElement("mail", colProps) & "</a>" ' this was used on an asp.net page so converts the email address to a clickable link, probably need to change that for vb.net
            aryElements(2) = GetElement("telephonenumber", colProps)
            aryElements(3) = GetElement("mobile", colProps)
            aryElements(4) = GetElement("facsimiletelephonenumber", colProps)
            aryElements(5) = GetElement("title", colProps)
            aryElements(6) = GetElement("description", colProps)
            aryElements(7) = GetElement("physicaldeliveryofficename", colProps)
            aryElements(8) = GetElement("streetaddress", colProps)
            aryElements(9) = GetElement("l", colProps)
            aryElements(10) = GetElement("postalcode", colProps)
            If GetElement("objectClass", colProps).IndexOf("computer") < 0 And aryElements(0).Length > 0 Then
                dtAD.Rows.Add(aryElements)
            End If
        Next
    End Sub

    Private Function GetElement(ByVal Key As String, ByRef Props As ResultPropertyCollection) As String
        Try
            If Props(Key) Is Nothing Then
                Return ""
            Else
                Dim aryProp() As String
                ReDim aryProp(Props.Item(Key).Count - 1)
                Props.Item(Key).CopyTo(aryProp, 0)
                Return Join(aryProp, vbCrLf)
            End If
        Catch ex As Exception
            Return ""
        End Try
    End Function

Tim Cottee
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