Export users from AD

  • Thread starter Thread starter Miha
  • Start date Start date
M

Miha

Guest
Hi guys,

I need a script that will export all users (with e-mail addresses) from AD
that have Exchange mailboxes into a .csv or plain text format. It needs to
be a script (not manual export) so that will run automatically with
scheduler.
How can this be done?
Thank you in advance.
Regards,
Miha
 
RE: Export users from AD

Hello Miha!

There are a lot of examples out there - just try Google search and you sure
will find some. You just have to change them for your special needs.

Best regards,

Gunter


"Miha" wrote:

> Hi guys,
>
> I need a script that will export all users (with e-mail addresses) from AD
> that have Exchange mailboxes into a .csv or plain text format. It needs to
> be a script (not manual export) so that will run automatically with
> scheduler.
> How can this be done?
> Thank you in advance.
> Regards,
> Miha
>
>
>
 
Re: Export users from AD

There are a number of ways to do this.

Using VBScript just pull the accounts from the OU you want to or the entire
domain. And get the data you want.

Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select sAMAccountName, sn, givenName, initials, description,
department, telephoneNumber, whenCreated, whenChanged, userAccountControl
from 'LDAP://DC=example,DC=org' " _
& "where objectClass='user' and objectCategory='person'"
objCommand.Properties("Page Size") = 2000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
wscript.echo objRecordSet.Fields("sAMAccountName")
objRecordSet.MoveNext
Loop

-- Just select the fields you want and echo the data the way you want.

You can also just use a simple bat file with the DS commands. Look up
dsquery and dsget in the examples you can see how to pipe your results.

And one more way would be to use a powershell script. This may be the best
option since you can easily format the output of your data.

Hope that helps,
Allan


"Miha" <miha.bernik@email.si> wrote in message
news:uQBV1JXxIHA.4864@TK2MSFTNGP06.phx.gbl...
> Hi guys,
>
> I need a script that will export all users (with e-mail addresses) from AD
> that have Exchange mailboxes into a .csv or plain text format. It needs to
> be a script (not manual export) so that will run automatically with
> scheduler.
> How can this be done?
> Thank you in advance.
> Regards,
> Miha
>
 
Re: Export users from AD


Hi Miha,

You can get it with a one-line command if you're willing to install PowerShell
and Quest's AD cmdlets:

Get-QADUser -sizeLimit 0 | where {$_.Email} | select name,Email | Export-Csv
mbx.csv


You can download PowerShell here:

http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx


And Quest's AD cmdlets, here:

http://www.quest.com/powershell/activeroles-server.aspx





---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

> Hi guys,
>
> I need a script that will export all users (with e-mail addresses)
> from AD
> that have Exchange mailboxes into a .csv or plain text format. It
> needs to
> be a script (not manual export) so that will run automatically with
> scheduler.
> How can this be done?
> Thank you in advance.
> Regards,
> Miha
 
Re: Export users from AD

Miha wrote:

> I need a script that will export all users (with e-mail addresses) from AD
> that have Exchange mailboxes into a .csv or plain text format. It needs to
> be a script (not manual export) so that will run automatically with
> scheduler.
> How can this be done?
> Thank you in advance.
> Regards,
> Miha


As noted, you can use ADO to retrieve information from AD. See this link:

http://www.rlmueller.net/ADOSearchTips.htm

The user name you retrieve can be the value of the "distinguishedName"
attribute or the "sAMAccountName" attribute. The later is also called the
"pre-Windows 2000 logon name" (or the NT Name). The "mail" attribute is the
E-mail address on the "General" tab of ADUC. However, if you use Exchange, I
think you want the value of the multi-valued attribute proxyAddresses.

Using the variables from the link above, the base of the search (if you want
all users in the domain) could be the distinguishedName of the domain:

' Search entire Active Directory domain.

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

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"



To retrieve information on all users the filter would be:



' Filter on user objects.
strFilter = "(&(objectCategory=person)(objectClass=user))"



To restrict the query to users with Exchange email addresses I believe the
filter would be:



' Filter on user objects with Exchange email addresses.
strFilter = "(&(objectCategory=person)(objectClass=user)(proxyAddresses=*))"



This retrieves all users that have any value(s) assigned to the
proxyAddresses attribute. Finally, the list of attribute values to retrieve
would be:



' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,distinguishedName,proxyAddresses"



I don't have Exchange so I cannot test, but the complete script could be as
below. The script echos the values to the console delimited with semicolons
(since distinguishedNames have embedded commas), so it can be read into a
spreasheet. Run the program at a command prompt with cscript and redirect
the output to a text file:

============

Option Explicit

Dim adoCommand, adoConnection, strBase, strFilter, strAttributes

Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strNTName, strDN

Dim strLine, arrAddresses, strAddress



' Setup ADO objects.

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection



' Search entire Active Directory domain.

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

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"


' Filter on user objects with Exchange email addresses.
strFilter = "(&(objectCategory=person)(objectClass=user)(proxyAddresses=*))"



' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,distinguishedName,proxyAddresses"



' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False



' Run the query.
Set adoRecordset = adoCommand.Execute


' Enumerate the resulting recordset.
Do Until adoRecordset.EOF

' Retrieve values and display.
strNTName = adoRecordset.Fields("sAMAccountName").Value

strDN = adoRecordset.Fields("distinguishedName").value

' Construct one line of output per user.

strLine = strNTName & ";" & strDN

' Add Email addresses to the output line.

arrAddresses = adoRecordset.Fields("proxyAddresses").Value

For Each strAddress In arrAddresses

strLine = strLine & ";" & strAddress

Next

Wscript.Echo strLine

' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop



' Clean up.

adoRecordset.Close

adoConnection.Close

===========

If the VBScript program is saved in a file called GetEmail.vbs, run it at a
command prompt with a command similar to:



cscript //nologo GetEmail.vbs > report.txt



The file report.txt is created in the current directory. It can be read into
a spreadsheet, just designate the semicolon as the delimiter.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
 
RE: Export users from AD


"Miha" wrote:

> Hi guys,
>
> I need a script that will export all users (with e-mail addresses) from AD
> that have Exchange mailboxes into a .csv or plain text format. It needs to
> be a script (not manual export) so that will run automatically with
> scheduler.
> How can this be done?
> Thank you in advance.
> Regards,
> Miha
>

or you can use dsget
http://technet2.microsoft.com/Windo...43fd-4985-b429-cd53d3046f011040.mspx?mfr=true

::bye Gas
@echo off
:: usr-email.cmd
:: get user list
set usrlist=c:\temp\UsrList.txt
type nul >%usrlist% 2>nul
for /f "skip=1 tokens=1,* delims= " %%A in ('dsquery user -name * -limit
0^|dsget user -samid -email^|find "@"') do echo %%A;%%B >>%usrlist%

--------------------------------------------------------------
Gastone Canali
http://www.experts-exchange.com/M_292401.html
--------------------------------------------------------------
 
Re: Export users from AD

Thank you guys! Have found a comand line syntax to export all smtp addresses
from AD, just what I was looking for

dsquery * forestroot -scope subtree -filter
"(&(mail=*)(proxyaddresses=smtp:*))" -attr proxyaddresses -limit 1000
>c:\addresses.csv


Regards,
Miha
"gas" <gas@discussions.microsoft.com> je napisal v sporoèilo
news:A10D2321-BCC9-43A1-9D54-16562C8600E8@microsoft.com ...
>
> "Miha" wrote:
>
>> Hi guys,
>>
>> I need a script that will export all users (with e-mail addresses) from
>> AD
>> that have Exchange mailboxes into a .csv or plain text format. It needs
>> to
>> be a script (not manual export) so that will run automatically with
>> scheduler.
>> How can this be done?
>> Thank you in advance.
>> Regards,
>> Miha
>>

> or you can use dsget
> http://technet2.microsoft.com/Windo...43fd-4985-b429-cd53d3046f011040.mspx?mfr=true
>
> ::bye Gas
> @echo off
> :: usr-email.cmd
> :: get user list
> set usrlist=c:\temp\UsrList.txt
> type nul >%usrlist% 2>nul
> for /f "skip=1 tokens=1,* delims= " %%A in ('dsquery user -name * -limit
> 0^|dsget user -samid -email^|find "@"') do echo %%A;%%B >>%usrlist%
>
> --------------------------------------------------------------
> Gastone Canali
> http://www.experts-exchange.com/M_292401.html
> --------------------------------------------------------------
>
>
 
Re: Export users from AD

ADFIND -b "<DN of some starting OU>" -f
"(&(objectCategory=person)(objectClass=user)(mail=*))" sAMAccountName mail

ADFIND can be downloaded from joeware.net

--

Cheers,
(HOPEFULLY THIS INFORMATION HELPS YOU!)

# Jorge de Almeida Pinto # MVP Identity & Access - Directory Services #

BLOG (WEB-BASED)--> http://blogs.dirteam.com/blogs/jorge/default.aspx
BLOG (RSS-FEEDS)--> http://blogs.dirteam.com/blogs/jorge/rss.aspx
------------------------------------------------------------------------------------------
* How to ask a question --> http://support.microsoft.com/?id=555375
------------------------------------------------------------------------------------------
* This posting is provided "AS IS" with no warranties and confers no rights!
* Always test ANY suggestion in a test environment before implementing!
------------------------------------------------------------------------------------------
#################################################
#################################################
------------------------------------------------------------------------------------------
"Miha" <miha.bernik@email.si> wrote in message
news:uQBV1JXxIHA.4864@TK2MSFTNGP06.phx.gbl...
> Hi guys,
>
> I need a script that will export all users (with e-mail addresses) from AD
> that have Exchange mailboxes into a .csv or plain text format. It needs to
> be a script (not manual export) so that will run automatically with
> scheduler.
> How can this be done?
> Thank you in advance.
> Regards,
> Miha
>
 
Back
Top