1

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!

1
  • Are you considering this powershell? Commented Dec 3, 2015 at 14:28

1 Answer 1

3

Your code is vbScript, but this is much easier to accomplish in powershell, and you did post in the powershell forum.

This will provide what you're looking for. Sometimes I prefer to use canonicalname over DistinguishedName.

This will require RSAT to be installed, or run from a domain controller.

import-module ActiveDirectory get-adcomputer -Filter 'Name -like "*"' -SearchBase "OU=starthere,DC=yourdom,DC=com" | select DistinguishedName get-adcomputer -Filter 'Name -like "*"' -SearchBase "OU=starthere,DC=yourdom,DC=com" -prop canonicalname | select canonicalname 
2
  • Sorry, I meant to add the VBScript tag too. I was hoping to get any type of script (PS, VBS, etc.) to get what I need. Thanks, Craig620. Commented Dec 3, 2015 at 14:58
  • If this does what you need, you can mark it as answered Commented Dec 3, 2015 at 15:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.