如何使用Windows API获取Active Directory的LDAP服务器URL?

时间:2014-03-13 13:03:54

标签: windows active-directory ldap

我一直在寻找一种从域用户运行的代码中获取Active Directory的LDAP服务器URL的方法。如果可能,代码需要在disjoint namespace的情况下正常工作。它是非托管代码,所以不幸的是,任何.NET解决方案都不是一个选项。

出于某种原因,无服务器绑定似乎在这种情况下无效,ADO查询在使用One or more errors occurred during processing of command时返回无用LDAP://DC=mycompany,DC=local错误(defaultNamingContext的值1}} rootDSE对象的属性。

使用LOGONSERVERUSERDNSDOMAIN环境变量似乎不是一个选项,因为代码也需要能够在SYSTEM帐户下运行,并且那里没有这样的变量

我们非常感谢任何想法或提示或特定的RTFM建议。

更新:DNSHostName的{​​{1}}属性似乎就是我需要的。

2 个答案:

答案 0 :(得分:1)

我使用 Visual Basic脚本(VBS)。将代码保存为.vbs文件并使用ANSI字符集。这个脚本很旧,但这可以指导您找到更好的解决方案。

Set cn = CreateObject("ADODB.Connection")
Set cmd= CreateObject("ADODB.Command")
cn.Provider = "ADsDSOObject;"
cn.open
cmd.ActiveConnection = cn

' Root DSE required to get the default configuration naming context to
' be used as the root of the seach
set objRootDSE = getobject("LDAP://RootDSE")
' Construct the LDAP query that will find all the domain controllers
' in the domain
ldapQuery = "<LDAP://" & objRootDSE.Get("ConfigurationNamingContext") & _
    ">;((objectClass=nTDSDSA));ADsPath;subtree"

cmd.CommandText = ldapQuery
cmd.Properties("Page Size") = 1000
Set rs = cmd.Execute

do while rs.EOF <> True and rs.BOF <> True
    ' Bind to the domain controller computer object
    ' (This is the parent object of the result from the query)
    set objDC = getobject(getobject(rs(0)).Parent)

    wscript.echo objDC.dNSHostName
        rs.MoveNext
Loop

cn.close

答案 1 :(得分:0)

rootDSE的DNSHostName属性似乎就是我需要的。