Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enumerate all users in AD - Not Working

Status
Not open for further replies.

keybrdcowboy

IS-IT--Management
Aug 31, 2004
96
US
Would appreciate any help you can give here. I am trying to loop through all our users in AD without feeding in usernames via a text file. Below is my code. When I run the code, it only counts 8xx users. We have a little over 28xxx user accounts in our domain. Can anyone help explain why this doesn't work? Thanks for any input.

Code:
Option Explicit

Dim oConn, oRS, oSid, strServerName, strGALEmail, strGALLastName, strGALFirstName, strGALDisplayName, strGALDescription, strLastChanged
Dim strGALCity, strGALOffice, strDomain, strGALZip, strGALState, strGALStreet, strGALTelNum, strGALTitle, strGALRank, oFSO, readFile
Dim outFile, iUpperBound, strUserName, oRootDSE, oConnection, oCommand, oRecordSet, strConnect, UserObj, strAD, strGoodCount
Dim strLogPath, oShell, strEmailLen, strSid, strTotalCount, strOKCount, strEmail, strQuery, strErrorControl, outFile2, strUser
Dim strChangedToday, strDidNotChangeToday

Const ForAppending = 8

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
strLogPath = oShell.ExpandEnvironmentStrings("%USERPROFILE%")

' Sets up the script to use a log file
If oFSO.FileExists(strLogPath & "\desktop\totalUsers.csv") = True Then
  Set outFile = oFSO.OpenTextFile(strLogPath & "\desktop\totalUsers.csv", ForAppending)
Else
  Set outFile = oFSO.CreateTextFile(strLogPath & "\desktop\totalUsers.csv", True) 
End If

strTotalCount = 0
  
  Set oRootDSE = GetObject("LDAP://rootDSE")
  Set oConnection = CreateObject("ADODB.Connection")
  oConnection.Open "Provider=ADsDSOObject;"
  Set oCommand = CreateObject("ADODB.Command")
  oCommand.ActiveConnection = oConnection
  
  oCommand.Properties ("Cache Results") = False
  oCommand.Properties ("Size Limit") = 100000
  oCommand.Properties ("Asynchronous") = True
  
  oCommand.CommandText = "<LDAP//" & oRootDSE.get("defaultNamingContext") & ">;(&(objectCategory=person)(objectClass=user));name;subtree"
  Set oRecordSet = oCommand.Execute
  
  While Not oRecordSet.EOF
    'outFile.WriteLine oRecordSet.Fields("name")
    strTotalCount = strTotalCount + 1
    oRecordSet.MoveNext
  Wend
  
  outFile.WriteLine "Total Count of Records: " & strTotalCount
  
  WScript.Echo "Done"

 
Try running this on your DC:

Code:
'==========================================================================
'
' NAME: <EnumerateUsers.vbs>
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 5/20/2004
'
' COMMENT: <comment>
'
'==========================================================================

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_UserAccount")
UserCount ="0"
For Each objItem in colItems
    Report = Report &  objItem.Name & vbCrLf
    UserCount = UserCount+1
Next

Report = "Total number of user=" & UserCount & vbCrLf & vbCrLf & Report
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile ("Users.txt", ForWriting)
ts.write Report
set ts = nothing
set fso = nothing
wscript.echo "Done"

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top