Re: Export list of users in each OU



I dont want to do the entire domain, but a specific container. I will see if
I can modify the script.

Thank you very much. I hope Microsoft is paying you for all the help you
give on these forums. Or at least gives you some free software.

"Richard Mueller [MVP]" wrote:

TJCooper1972 wrote:

Can someone please point me to a script that creates an spread*** that
list
OUs and accounts in each container. For example:

Accounts
SC
Tim
Tom
Tammy

Perhaps a workboot for each container under accounts. If someone can point
me to something close, I can usually modify it.

The first step is to enumerate all containers (domains, OU's, and container
objects). You can either use ADO or code a recursive method. In either case
you enumerate objects with class "container", "organizationalUnit", or
"builtinDomain". Next you bind to each container object, filter on objects
of class user, and enumerate. For example (not tested):
==============
Option Explicit
Dim objRootDSE, strForest, objForest

Set objRootDSE = GetObject("LDAP://RootDSE";)
strForest = objRootDSE.Get("rootDomainNamingContext")
Set objForest = GetObject("LDAP://"; & strForest)

Call EnumDomains(objForest)

Sub EnumDomains(ByVal objParent)
' Recursive subroutine to enumerate users in domains.
Dim objUser, objContainer, objChild

' Enumerate users in domain.
objParent.Filter = Array("user")
For Each objUser In objParent
Wscript.Echo objParent.distinguishedName & ";" &
objUser.sAMAccountName
Next

' Enumerate containers in domain.
objParent.Filter = Array("container", "organizationalUnit",
"builtinDomain")
For Each objContainer In objParent
Call EnumContainers(objContainer)
Next

' Enumerate child domains.
objParent.Filter = Array("domain")
For Each objChild In objParent
Call EnumDomains(objChild)
Next

End Sub

Sub EnumContainers(ByVal objParent)
' Recursive subroutine to enumerate users in containers.
Dim objUser, objChild

' Enumerate users in container.
objParent.Filter = Array("user")
For Each objUser In objParent
Wscript.Echo objParent.distinguishedName & ";" &
objUser.sAMAccountName
Next

' Enumerate child containers.
objParent.Filter = Array("container", "organizationalUnit",
"builtinDomain")
For Each objChild In objParent
Call EnumContainers(objChild)
Next

End Sub
============
The script should be run at a command prompt with the cscript host. The
output can be redirected to a text file. The text file can be imported into
a spread*** program. The fields are semicolon delimited. The above
documents the distinguishedNames (DN's) of the domains, containers, and OU's
(the only way to uniquely identify them) and the NT names (pre-Windows 2000
logon names) of the users. You could document the distinguishedNames of
users instead. Or, since the DN of the parent container is in the first
column, you could even use the value of the cn attribute (Common Name).

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--



.


Quantcast