Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBscirpt to list all user's DN into a txt file

Posted on 2010-09-16
12
Medium Priority
?
706 Views
Last Modified: 2013-12-24
Hi Guys,

I need a vbscript to read the root of my domain and query all user's DN value then write to a text file with each user's DN line by line.

Please help! Thanks in advance for your help!
0
Comment
Question by:changjia
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33696463
change ROOT_LDAP to the ldap from which to get users DN.
const ROOT_LDAP = "cn=users,dc=soap,dc=com"
const USERS_FILE = "c:\temp\users.log"
Dim oConnection, oCommand, oRecordSet

Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & ROOT_LDAP & ">;(&(objectCategory=User));distinguishedName;subtree"
Set objRecordSet = oCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
	line = line & objRecordSet.Fields("DistinguishedName") & vbNewLine
	objRecordSet.MoveNext
Loop

Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.CreateTextFile(USERS_FILE, 2)
objFile.Write line
objFile.Close
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing

Open in new window

0
 
LVL 17

Expert Comment

by:Tony Massa
ID: 33696510
ADFIND Use the following command:
adfind -default -f objectClass=User -list distinguishedName

DSQUERY:  Use the following command.  Default limit is 100 (if you have less than 5000 users):
dsquery user -limit 5000
0
 

Author Comment

by:changjia
ID: 33696586
Hi Sedgwik,

I received error at line 12, charactor 1.

Thanks
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
LVL 17

Accepted Solution

by:
Tony Massa earned 1000 total points
ID: 33696604
Here's one that I have...it doesn't write to a log, but you can run with CSCRIPT and output (with >) to a file.


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.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

objCommand.CommandText = _
    "SELECT distinguishedName FROM 'LDAP://dc=soap,dc=com' WHERE objectCategory='user'"  
Set objRecordSet = objCommand.Execute

On Error Resume Next

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
  wscript.echo objRecordSet.Fields("distinguishedName").Value
	objRecordSet.MoveNext
Loop

Open in new window

0
 

Author Comment

by:changjia
ID: 33696620
nevermind, I got that part fix, however,the output is partial, it doens't seem to grab all users in my domain, only 10%.
0
 

Author Comment

by:changjia
ID: 33696646
It looks like only retrive the first 1000 records, I want it all.. Thanks
0
 
LVL 42

Assisted Solution

by:Meir Rivkin
Meir Rivkin earned 1000 total points
ID: 33696782
try now
const ROOT_LDAP = "cn=users,dc=soap,dc=com"
const USERS_FILE = "c:\temp\users.log"
Dim oConnection, oCommand, objRecordSet

Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.Properties("Page Size") = 1000
oCommand.CommandText = "<LDAP://" & ROOT_LDAP & ">;(&(objectCategory=User));distinguishedName;subtree"
Set objRecordSet = oCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
	line = line & objRecordSet.Fields("DistinguishedName") & vbNewLine
	objRecordSet.MoveNext
Loop

Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.CreateTextFile(USERS_FILE, 2)
objFile.Write line
objFile.Close
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing

Open in new window

0
 

Author Comment

by:changjia
ID: 33696811
samthing, only the first 1000 records were listed. Also, it seems not only retriving users, but also groups. I want users only. Thanks
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33696870
this script checks for users only (see line 10)
i've tested it and it works fine, it logs 1215 users and only users.
can u post the exact script u running cause i'm not sure how it is possible

0
 

Author Comment

by:changjia
ID: 33696985
you are right, the script works, Thanks for your help!!
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 34608867
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this article, we’ll look at how to deploy ProxySQL.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

810 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