Solved

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

Posted on 2010-09-16
12
672 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
12 Comments
 
LVL 42

Expert Comment

by:sedgwick
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 17

Accepted Solution

by:
Tony Massa earned 250 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:sedgwick
sedgwick earned 250 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:sedgwick
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 70

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now