批量导出AD组内的成员

网上搜来的代码,自己记录下来方面以后使用

把下面代码存为*.vbs,把红字体部分改为你的OU和DC即可。


Const ADS_SCOPE_SUBTREE = 2


Set objConnection = CreateObject("ADODB.Connection")

Set objCommand = CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"


Set objCOmmand.ActiveConnection = objConnection

'LDAP指定对象的来源, objectclass=group指定类型,Name, Mail都是用户帐号对象的属性

objCommand.CommandText = _

    "Select Name, mail, distinguishedName from 'LDAP://OU=Domain Local Groups,OU=Groups,dc=test,dc=com' " _

        & "Where objectClass='group'"  

objCommand.Properties("Page Size") = 1000

objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst


'输出文件的名字叫group.csv

Set objFS = CreateObject("Scripting.FileSystemObject")

Set objNewFile = objFS.CreateTextFile("group.csv")

objNewFile.WriteLine "Group Name, Group Member, Mail"


Do Until objRecordSet.EOF

    objNewFile.WriteLine

'输出组的名称

 objNewFile.WriteLine objRecordSet.Fields("Name").Value & " ," & " ," &_

 objRecordSet.Fields("mail").Value

'引用组的名称

 Set objGroup = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName").Value)

'列出组成员 

  For each objMember in objGroup.Members

   objNewFile.WriteLine " ," & objMember.Name & " ," & objMember.description

  Next

 objRecordSet.MoveNext

Loop


你可能感兴趣的:(批量导出AD组内的成员)