从Workgroup查询AD用户信息

以前写过一段脚本是从AD域中查询AD用户信息。但是如果从WorkGroup上就无法得到结果了。下面的一段脚本可以实现从WorkGroup查询域账户信息。

 

注意:运行之前先将DC服务器以及账户名和密码替换,然后保存成VBS。如果要查询其他信息可以更改对应的Filter,category和Class.

 

Option Explicit

 Dim objRootDSE, strDNSDomain, adoCommand, adoConnection
 Dim strBase, strFilter, strAttributes, strQuery, adoRecordset
 Dim strDN, objNS, strServer

 Const ADS_SECURE_AUTHENTICATION = &H1
 Const ADS_SERVER_BIND = &H200

 ' Specify a server (Domain Controller).
 strServer = "Allen.home"


 ' Determine DNS domain name. Use server binding and alternate
 ' credentials. The value of strDNSDomain can also be hard coded.
 Set objNS = GetObject("LDAP:")
 Set objRootDSE = objNS.OpenDSObject("LDAP://" & strServer & "/RootDSE", _
      strUser, strPassword, _
      ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION)
 strDNSDomain = objRootDSE.Get("defaultNamingContext")

 ' Use ADO to search Active Directory.
 ' Use alternate credentials.
 Set adoCommand = CreateObject("ADODB.Command")
 Set adoConnection = CreateObject("ADODB.Connection")
 adoConnection.Provider = "ADsDSOObject"
 adoConnection.Properties("User ID") = "youruser"
 adoConnection.Properties("Password") = "yourPassword"
 adoConnection.Properties("Encrypt Password") = True
 adoConnection.Properties("ADSI Flag") = ADS_SERVER_BIND _
      Or ADS_SECURE_AUTHENTICATION
 adoConnection.Open "Active Directory Provider"
 Set adoCommand.ActiveConnection = adoConnection

 ' Search entire domain. Use server binding.
 strBase = "<LDAP://" & strServer & "/" & strDNSDomain & ">"

 ' Search for special users.
 strFilter = "(&(objectCategory=person)(objectClass=user)(samaccountname=mycode))"

 'only get display name for user
 strAttributes = "name"

 ' Construct the LDAP query.
 strQuery = strBase & ";" & strFilter & ";" _
      & strAttributes & ";subtree"

 ' Run the query.
 adoCommand.CommandText = strQuery
 adoCommand.Properties("Page Size") = 100
 adoCommand.Properties("Timeout") = 30
 adoCommand.Properties("Cache Results") = False
 Set adoRecordset = adoCommand.Execute

 ' Enumerating the result(for me only have one result)
 Do Until adoRecordset.EOF
      ' Retrieve values.
      strDN = adoRecordset.Fields("name").Value
      Wscript.Echo strDN
      adoRecordset.MoveNext
 Loop

 ' Clean up.
 adoRecordset.Close
 adoConnection.Close

 

更多信息可以参考:

Searching with ActiveX Data Objects (ADO)

http://msdn.microsoft.com/en-us/library/Aa746471.aspx

Search Filter Syntax

http://msdn.microsoft.com/en-us/library/Aa746475.aspx

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值