简体   繁体   English

使用VBScript查询Active Directory

[英]Querying Active Directory using VBScript

I want to query Active Directory using VBScript (classic ASP). 我想使用VBScript(经典ASP)查询Active Directory How can I accomplish that? 我怎么能做到这一点?

To look at all the members of an OU, try this... 要查看OU的所有成员,请尝试以下操作...

Set objOU = GetObject("LDAP://OU=YourOU,DC=YourDomain,DC=com")
For each objMember in ObjOU  ' get all the members'

    ' do something'

Next

To do a custom search for DNs try this... 要对DN进行自定义搜索,请尝试以下操作...

set conn = createobject("ADODB.Connection")
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strDefaultNamingContext = iAdRootDSE.Get("defaultNamingContext")
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"

strQueryDL = "<LDAP://" & strDefaultNamingContext & ">;(&(objectCategory=person)(objectClass=user));distinguishedName,adspath;subtree"
set objCmd = createobject("ADODB.Command")
objCmd.ActiveConnection = Conn
objCmd.Properties("SearchScope") = 2 ' we want to search everything
objCmd.Properties("Page Size") = 500 ' and we want our records in lots of 500 

objCmd.CommandText = strQueryDL
Set objRs = objCmd.Execute

While Not objRS.eof

    ' do something with objRS.Fields("distinguishedName")'
    objRS.MoveNext
Wend

I had to query WinAD by oldskool username, this .vbs script prints user accounts. 我不得不通过oldskool用户名查询WinAD,这个.vbs脚本打印用户帐户。

  • find by sAMAccountname, use * wildcard 通过sAMAccountname查找,使用*通配符
  • print few attributes from each user object 从每个用户对象打印几个属性
  • use AccountType filter its most optimized way of iterating AD user objects 使用AccountType过滤器最优化的迭代AD用户对象的方法

Test script first gets an user object by fully qualified string, its just an example. 测试脚本首先通过完全限定的字符串获取用户对象,这只是一个示例。 Second part does actual query by smith* filter. 第二部分通过smith * filter进行实际查询。

WinADSearch.vbs WinADSearch.vbs

' c:> cscript -nologo script.vbs
' c:> wscript script.vbs
' http://msdn.microsoft.com/en-us/library/d6dw7aeh%28v=vs.85%29.aspx

' WindowsAD queries
' http://www.kouti.com/tables/userattributes.htm

Option Explicit
'On Error Resume Next

Dim StdOut: Set StdOut = WScript.StdOut

Dim objUser
Set objUser = GetObject("LDAP://CN=Firstname Lastname,OU=Internal Users,OU=MyCompany,OU=Boston,OU=Root,DC=REGION1,DC=COM")
println(objUser.givenName & " " & objUser.middleName & " " & objUser.lastName) 
println("name=" & objUser.name)
println("displayName=" & objUser.displayName)
println("userPrincipalName=" & objUser.userPrincipalName)
println("sAMAccountName=" & objUser.sAMAccountName)
println("distinguishedName=" & objUser.distinguishedName)


println("")
Dim conn, strQueryDL, strAttrs, objCmd, objRs, idx

set conn = createobject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
conn.Open "ADs Provider"

strAttrs = "sAMAccountName,displayName,distinguishedName" ' get attributes

'strQueryDL = "<LDAP://dc=REGION1,dc=COM>;(& (objectCategory=person) );" & strAttrs & ";SubTree"
'strQueryDL = "<LDAP://dc=REGION1,dc=COM>;(& (objectCategory=person)(objectClass=user) );" & strAttrs & ";SubTree"    
'strQueryDL = "<LDAP://dc=REGION1,dc=COM>;(& (objectCategory=person)(objectClass=user)(sAMAccountName=smith*) );" & strAttrs & ";SubTree"

strQueryDL = "<LDAP://dc=REGION1,dc=COM>;(& (samAccountType=805306368)(sAMAccountName=smith*) );" & strAttrs & ";SubTree"

set objCmd = createobject("ADODB.Command")
objCmd.ActiveConnection = Conn
objCmd.Properties("SearchScope") = 2 ' search everything
objCmd.Properties("Page Size") = 100 ' bulk operation

objCmd.CommandText = strQueryDL
println(objCmd.CommandText)
Set objRs = objCmd.Execute
idx=0
do while Not objRS.eof
  idx=idx+1
  println( objRs.Fields("sAMAccountName") & " / " & objRs.Fields("displayName") & " / " & objRs.Fields("distinguishedName") )
  if (idx>5) then exit do
  objRS.MoveNext
loop
objRs.Close
Conn.close
set objRs = Nothing
set conn = Nothing
println("end")


'********************************************************************
Sub println(ByVal str) 
    If (StdOut Is Nothing) Then Exit Sub
    StdOut.WriteLine str
End Sub

You want to use Active Directory Service Interfaces (ADSI) 您想使用Active Directory服务接口(ADSI)

The ADSI Scripting Primer is a good place to start learning and find examples. ADSI Scripting Primer是开始学习和查找示例的好地方。 (btw, these links refer to Windows 2000, but are valid for subsequent versions of Windows as well). (顺便说一句,这些链接指的是Windows 2000,但也适用于后续版本的Windows)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM