I’m trying to understand where I’m going wrong here. The following script will always return an empty item in the array at the end. Why? I don’t think it’s a problem with the recordset. Any ideas?
function allServers
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=computer)(operatingsystem=*server*)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
strAttributes = "name,distinguishedname,dnshostname"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
dim strList, i
Do Until adoRecordset.EOF
strList = strList & adoRecordSet.Fields("name").Value & ","
adoRecordset.MoveNext
loop
adoRecordset.Close
adoConnection.Close
arr = split(strList, ",",-1,1)
allServers = arr
end function
arr = allservers
For i = 0 to UBound(arr)
wscript.echo i & ":" & arr(i)
next
Your loop:
puts a last/trailing/terminating “,” at the end of strList. Split() treats the “,” as separator (not terminator), so the resulting array has a spurious last empty element.
To avoid that, you can concatenate
"," & valuein the loop and zap the preceding “,” using Mid(). But why not use “SELECT COUNT”, or Recordset.Count to dimension an array before the loop and assign the values to its elements, or – even better – Recordset.GetRows() to get an (albeit 2 dim) array directly -> no concat, no split, no problem.