# AD query dumped into csv in 3 columns.



## StumpedTechy (Jul 7, 2004)

I need some kind of vbs script that pulls the first name, last name, and email address (not LDAP or any crazy type of information just [email protected]) of all active users AND contacts (can't be querying those that are disabled though). I then need all of this information dumped (the 3 items first, last, email) into 3 columns on a CSV.

Anyone able to help I don't have time to think this one over and was hoping someone already had something that queried AD. I have done some internet searching and find alot of things that aren't quite fitting my bill.


----------



## stod73 (Aug 13, 2003)

I would use ADUC, create a query, and export the list. The query shoudl be very simple to create and you can add any columns of needed info to the queried data.


----------



## StumpedTechy (Jul 7, 2004)

> I would use ADUC, create a query


like I said this is what I need help with... I have never written a script to query AD before.


----------



## StumpedTechy (Jul 7, 2004)

I tried using - csvde -r "(objectClass=user)" -l givenName,sn,mail -o DN -f c:\users3.csv - and this gave me all of my information I need but it also gives me users with blank names and email addresses.

I really want an automated list I don't have to edit each time I do an export as this will be done frequently.


----------



## StumpedTechy (Jul 7, 2004)

BTW I found it -
Can anyone tell me now how to make this so it not only queries users but also contacts... I tried added an EnumContacts but I am not sure if thats even possible... anyone able to shed some light?


```
Dim rootDSE, domain

Set rootDSE = GetObject("LDAP://RootDSE")

Set domain = GetObject("LDAP://" & rootDSE.Get("defaultNamingContext"))

Set fso = CreateObject("Scripting.FileSystemObject")
Set outfile = fso.CreateTextFile("GAL List.csv")

Call EnumUsers(domain.ADsPath)
Call EnumOUs(domain.ADsPath)

Sub EnumOUs(adspath)
    Dim container, ou

    Set container = GetObject(adspath)

    container.Filter = Array("OrganizationalUnit")

    Call EnumUsers(container.ADsPath)

    For Each ou In container

        Call EnumOUs(ou.ADsPath)

    Next


End Sub


Sub EnumUsers(adspath)
    Dim container, user

    Set container = GetObject(adspath)

    container.Filter = Array("User")

    For Each user In container

        If user.Mail <> "" and user.sn <> "" and user.givenName <> "" Then

            outfile.WriteLine user.sn & ", " & user.givenName & ", " & user.Mail

        End If

    Next


End Sub

msgbox "Your GAL export is completed to file GAL List.csv"
```


----------



## StumpedTechy (Jul 7, 2004)

Why is it I always feel like I am talking to myself in this particular forum.

Anyways its all solved heres the final code that was created. The only thing I have to do is add in a request for it to not use disabled accounts... but thats easy enough.



> Dim rootDSE, domain
> 
> Set rootDSE = GetObject("LDAP://RootDSE")
> 
> ...


----------

