Solved

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

Posted on 2010-09-16
12
653 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now