I have an OU with many sub-OUs, each with an OU for computers. The highest level OU to start is 'Departments' with many sub-OU's.
I need a script to retrieve all computers and list them in a table with their respective OU displayed.
The result of the script might look something like this:
Computer Name --- OU
Computer01 ---- Fabrikam.com/Departments/Finance/Computers
....or it might even look like it's corresponding AD 'Object':
Fabrikam.com/Departments/Finance/Computers/Computer01
Although I much prefer the first one.
This is the script I've tried, but it throws out tons of errors when I run it:
On Error Resume Next 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 ADsPath FROM 'LDAP://OU=Departments,dc=fabrikam,dc=com' WHERE " & _ "objectCategory='organizationalUnit'" Set objRecordSet = objCommand.Execute objRecordSet.MoveFirst Do Until objRecordSet.EOF Set objOU = GetObject(objRecordSet.Fields("ADsPath").Value) Wscript.Echo objOU.distinguishedName objOU.Filter = Array("Computer") For Each objItem in objOU Wscript.Echo " " & objItem.CN Next Wscript.Echo Wscript.Echo objRecordSet.MoveNext Loop
How can I retrieve all computers in an OU and have them listed in a table with their respective OU's? Seems simple, but I can't find a working script anywhere. Thanks!