Solved

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

Posted on 2010-09-16
12
680 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…

831 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