连接和拆分返回 ubound(array) + 1 件
我试图理解我哪里出了问题。以下脚本将始终在数组末尾返回一个空项。为什么?我不认为这是记录集的问题。有什么想法吗?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的循环:
在 strList 的末尾放置最后一个/尾随/终止“,”。 Split() 将“,”视为分隔符(而不是终止符),因此生成的数组有一个虚假的最后一个空元素。
为了避免这种情况,您可以连接
"," & value
在循环中并使用 Mid() 取消前面的“,”。但是为什么不在循环之前使用“SELECT COUNT”或 Recordset.Count 来确定数组的尺寸并将值分配给其元素,或者 - 甚至更好 - Recordset.GetRows() 直接获取一个(尽管是 2 个暗淡)数组 -> ;没有连接,没有分割,没有问题。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
"," & value
in 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.