Question : Script that displays all devices in Active Directory

Hello,

I found this cool script on Experts Exchange that is supposed to query Active Directory for all the devices in the domain and output the results to a file called "output.txt".  For the most part the script works but it will not find the devices located under Domain Controllers or Computers container.  However, it will find all the devices located in every other container in my domain no matter what level in teh tree.  

Can someone look over the script and fix it so it will search for any device in Active Directory.

Thanks,

Capt.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
Const OUTPUT_FILE = "out.txt"
 
Dim objConnection : Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
 
Dim objCommand : Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
 
' Get the current Domain to use with the Search Base
'Dim objRootDSE : Set objRootDSE = GetObject("LDAP://RootDSE")
'Dim strDomainDN : strDomainDN = objRootDSE.Get("defaultNamingContext")
'Set objRootDSE = Nothing
 
' Run the search from here down
Dim strBase : strBase = ""
 
' The LDAP Filter to return computers who are in Domain Computers, but nothing else
Dim strFilter : strFilter = "(&(objectCategory=computer)(primaryGroupID=515)(!memberOf=*))"
 
' List all attributes required
Dim strAttributes : strAttributes = "distinguishedName,CN"
 
Dim strQuery : strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
 
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 999999
objCommand.Properties("Timeout") = 300
objCommand.Properties("Cache Results") = False
 
Set objRecordSet = objCommand.Execute
 
' Set up a file to write the results to
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.OpenTextFile(OUTPUT_FILE, 2, True, 0)
 
Do Until objRecordSet.EOF
  objFile.WriteLine objRecordSet.Fields("CN").Value
 
  objRecordSet.MoveNext
Loop
 
Set objFile = Nothing
Set objFSO = Nothing
 
Set objRecordSet = Nothing
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing

Answer : Script that displays all devices in Active Directory

You need to replace line 19 with:

Dim strFilter : strFilter = "(&(objectCategory=computer))"
Random Solutions  
 
programming4us programming4us