VBS LDAP查询问题(vbs ldap query issues)

2019-10-17 05:16发布

目前,我试图用我的脚本的小知识,通过LDAP来搜索查找基于一个变量,然后让我的该用户的显示名用户。 到目前为止,我ahve下面和IM卡

On Error Resume Next

Dim objNetwork 
Dim userName   

  Set objNetwork = CreateObject("WScript.Network") 
  userName = objNetwork.UserName   
  WScript.Echo userName 


  Set objConnection = CreateObject("ADODB.Connection")
 Set objCommand =   CreateObject("ADODB.Command")
 objConnection.Provider = "ADsDSOObject"
 objConnection.Open "Active Directory Provider"
 Set objCommand.ActiveConnection = objConnection

     objCommand.Properties("Page Size") = 1000

        objCommand.CommandText =  _
  "<LDAP://dc=domain,dc=com>;(objectCategory=User);Name;Subtree"  
Set objRecordSet = objCommand.Execute

 objRecordSet.MoveFirst

  Do Until objRecordSet.Fields("Name").Value = username

  objRecordSet.MoveNext
  Loop

  Wscript.Echo objRecordSet.Fields("Name").Value

=请帮助!

我scartached以上,我现在想这个,但得到的语法错误

option explicit dim cmd, cn, rs, objRoot,
set cmd = createobject("ADODB.Command") 
    set cn = createobject("ADODB.Connection") 

set rs = createobject("ADODB.Recordset") 
cn.open "Provider=ADsDSOObject;" 

cmd.activeconnection = cn set 

objRoot = getobject("LDAP://RootDSE") cmd.commandtext = "<LDAP://dn=domain,dn=com">;(&(objectClass=user)(!(objectClass=computer)            (sAMAccountName=first.last))));name,displayname;subtree"

cmd.properties("page size")=1000 set rs = cmd.execute 

wscript.echo rs("name") 

Answer 1:

我怀疑属性名称名称,并尝试在显示名,而不是这一行:

"<LDAP://dc=domain,dc=com>;(objectCategory=User);Name;Subtree" 


Answer 2:

结束了这个让

设置objConnection =的CreateObject( “ADODB.Connection”)

Set objCommand = CreateObject("ADODB.Command")  

objConnection.Open "Provider=ADsDSOObject;" 
objCommand.ActiveConnection = objConnection  

strDomainName = "dc=domain,dc=com" 
strUserCN = username 
objCommand.CommandText = "<LDAP://" & strDomainName & ">;(&(objectCategory=person)     (objectClass=user)(cn=" & strUserCN & "));displayName;subtree"  

Set objRecordSet = objCommand.Execute  

If Not objRecordset.EOF Then  

strray = split(objRecordSet.Fields("displayName"),"(") 
strdat = strray(0)



username = trim(strdat)

End If  


文章来源: vbs ldap query issues