简体   繁体   English

使用 VBScript/WSH 查询 Active Directory 和导出

[英]Query Active Directory and Export using VBScript/WSH

I want to query a AD server to get certain fields using VBScript or WSH script我想查询 AD 服务器以使用 VBScript 或 WSH 脚本获取某些字段

These fields这些领域

  • DN DN
  • userid用户身份
  • mail邮件
  • company公司
  • displayName显示名称

And export/output the fields to a text file.并将字段导出/输出到文本文件。

How can I accomplish that?我怎样才能做到这一点?

I came from linux background and need this as the computer that will run the script is running windows我来自 linux 背景并且需要这个,因为将运行脚本的计算机正在运行 windows

Using linux,使用Linux,

`ldapsearch -x -h hostserver -b "cn=contacts,dc=support,dc=com" CN="name"`

The usual method is to use ADO and an LDAP query to retrieve information about AD objects:通常的方法是使用 ADO 和LDAP 查询来检索有关 AD 对象的信息:

Set rootDSE = GetObject("LDAP://RootDSE")

base   = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"
filter = "(&(objectClass=user)(objectCategory=Person))"
attr   = "distinguishedName,userid,mail,company,displayName"
scope  = "subtree"

Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
conn.Open "Active Directory Provider"

Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = base & ";" & filter & ";" & attr & ";" & scope

Set rs = cmd.Execute
Do Until rs.EOF
  'do stuff with rs.Fields(fieldname).Value
  rs.MoveNext
Loop
rs.Close

conn.Close

As you can see there is a lot of boilerplate code involved, so I wrote this class ( ADQuery ) to simplify the handling.如您所见,其中涉及大量样板代码,因此我编写了此类( ADQuery ) 以简化处理。 The README contains some examples.自述文件包含一些示例。

If you already know the distinguished name of an object you can also directly retrieve it like this:如果您已经知道对象的专有名称,您也可以像这样直接检索它:

dn = "CN=Joe User,OU=Users,DC=example,DC=com"
Set user = GetObject("LDAP://" & dn)
WScript.Echo user.Get("displayName")
WScript.Echo user.Get("mail")
...
attr = "distinguishedName,userid,mail,company,displayName"

userid is not part of AD, it is sAMAccountName userid 不是 AD 的一部分,它是 sAMAccountName

Set cmd.CommandText = base & ";" & filter & ";" & attr & ";" & scope

cmd.CommandText is not an object, it is only: cmd.CommandText = base & ";" cmd.CommandText 不是一个对象,它只是: cmd.CommandText = base & ";" & filter & ";" & 筛选 & ”;” & attr & ";" & attr & ";" & scope & 范围

You need to use the DirectoryServices namespace .您需要使用 DirectoryServices 命名空间 This will give you access to the AD and the required AP to query it.这将使您能够访问 AD 和查询它所需的 AP。 Directory Searcher object can be used to quert the AD and it returns a SerchResult collection . Directory Searcher对象可用于查询 AD 并返回一个SerchResult 集合 You can then read the values out of that as strings, easily writable into a text file.然后,您可以将其中的值作为字符串读取,轻松写入文本文件。 Hope this was helpful!希望这是有帮助的!

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

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