连接和拆分返回 ubound(array) + 1 件

发布于 2024-11-27 01:58:21 字数 1398 浏览 1 评论 0原文

我试图理解我哪里出了问题。以下脚本将始终在数组末尾返回一个空项。为什么?我不认为这是记录集的问题。有什么想法吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2024-12-04 01:58:21

您的循环:

Do Until adoRecordset.EOF
    strList = strList & adoRecordSet.Fields("name").Value & ","
    adoRecordset.MoveNext
loop

在 strList 的末尾放置最后一个/尾随/终止“,”。 Split() 将“,”视为分隔符(而不是终止符),因此生成的数组有一个虚假的最后一个空元素。

为了避免这种情况,您可以连接 "," & value 在循环中并使用 Mid() 取消前面的“,”。但是为什么不在循环之前使用“SELECT COUNT”或 Recordset.Count 来确定数组的尺寸并将值分配给其元素,或者 - 甚至更好 - Recordset.GetRows() 直接获取一个(尽管是 2 个暗淡)数组 -> ;没有连接,没有分割,没有问题。

Your loop:

Do Until adoRecordset.EOF
    strList = strList & adoRecordSet.Fields("name").Value & ","
    adoRecordset.MoveNext
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文