Submitted By: Paul Cook

Creates a disconnected dataset that holds the names of all the computers in an OU or set of OUs (just add more OUs to the array). A disconnected recordset is easier to sort than an array.

Visual Basic
Edit|Remove
On Error Resume Next
Const adVarChar = 200
Const MaxCharacters = 255

Set objRootDSE = GetObject("LDAP://rootDSE")
Set DataList = CreateObject("ADODB.Recordset")

' Add field title
DataList.Fields.Append "ServerName", adVarChar, MaxCharacters
' Open dataset
DataList.Open

' Get domain name
strDomain = objRootDSE.Get("DefaultNamingContext")
' Create array with ous you wish to use
arrServer = Array("Domain Controllers", "Servers", "Terminal Servers")
' Loop through ous
For Each OU In arrServer
' Connect to individual ous
	Set objConfiguration = GetObject("LDAP://ou=" & OU & "," & strDomain)
' Filter on computers only
	objConfiguration.Filter = Array("computer")
' Loop through entities
	For Each objContainer in objConfiguration
' Add server to dataset
		DataList.AddNew 
		DataList("ServerName") = objContainer.CN
		DataList.Update 
	Next
Next

' Sort dataset
DataList.Sort = "ServerName" 
' Go to beginning of list
DataList.MoveFirst 

' Loop through dataset
Do Until DataList.EOF
	WScript.Echo DataList.Fields.Item("ServerName")
	DataList.MoveNext
Loop