如何获取Active Directory';使用windows API的LDAP服务器url?

如何获取Active Directory';使用windows API的LDAP服务器url?,windows,active-directory,ldap,Windows,Active Directory,Ldap,我一直在寻找一种从以域用户身份运行的代码中获取Active Directory的LDAP服务器url的方法。如果可能,代码需要在以下情况下正常工作:。它是非托管代码,因此任何.NET解决方案都不是一个选项 由于某种原因,在这种情况下,无服务器绑定似乎不起作用,ADO查询返回无用的在处理命令过程中出现一个或多个错误使用LDAP://DC=mycompany,DC=local(这是rootDSE对象的defaultNamingContext属性的值) 使用LOGONSERVER和USERDNSDOM

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

由于某种原因,在这种情况下,无服务器绑定似乎不起作用,ADO查询返回无用的
在处理命令过程中出现一个或多个错误
使用
LDAP://DC=mycompany,DC=local
(这是
rootDSE
对象的
defaultNamingContext
属性的值)

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

任何想法或提示或具体的RTFM建议将不胜感激


更新:我需要
rootDSE的
DNSHostName
属性。

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

我使用这个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
Set cn=CreateObject(“ADODB.Connection”)
Set cmd=CreateObject(“ADODB.Command”)
cn.Provider=“ADsDSOObject;”
中国公开
cmd.ActiveConnection=cn
'获取默认配置命名上下文所需的根DSE
"被用作山根"
设置objRootDSE=getobject(“LDAP://RootDSE”)
'构造将查找所有域控制器的LDAP查询
“在域中
ldapQuery=“;((objectClass=nTDSDSA));ADsPath;子树”
cmd.CommandText=ldapQuery
cmd.Properties(“页面大小”)=1000
设置rs=cmd.Execute
当rs.EOF为真和rs.BOF为真时执行
'绑定到域控制器计算机对象
'(这是查询结果的父对象)
设置objDC=getobject(getobject(rs(0)).Parent)
wscript.echo objDC.dNSHostName
下一个
环
cn.close

谢谢您的建议!