【问题标题】:VBScript to export all members of multiple Active Directory groups?VBScript 导出多个 Active Directory 组的所有成员?
【发布时间】:2011-07-04 10:57:40
【问题描述】:

有没有一种方法可以使用 VBScript 一次导出多个 Active Directory 组的所有成员?最好输出是在他们所属的组下列出的用户名。

我有以下允许我一次导出 1 个 AD 组的成员,但我不知道如何修改它以查看多个组。

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set outfile = fso.CreateTextFile("Members.csv")
Set objGroup = GetObject("LDAP://cn=*GROUPNAME*,OU=Groups,DC=domain,DC=local")
objGroup.GetInfo

arrMembersOf = objGroup.GetEx("member")

For Each GetObject in ObjGroup
    outfile.WriteLine objGroup.Name
Next

For Each strMember in arrMembersOf
    outfile.WriteLine strMember
Next

有什么想法吗?

【问题讨论】:

    标签: vbscript active-directory ldap


    【解决方案1】:

    是的,这是可能的,但我认为您可能需要稍微改变您的方法。您需要编写一个 LDAP 查询来一次查询两个组,而不仅仅是将范围设置为特定组。

    所以,试着像这样修改你的脚本:

      Set objRootDSE = GetObject("LDAP://RootDSE")
        strDNSDomain = objRootDSE.Get("defaultNamingContext")
      Set objRootDSE = Nothing
      Set ad = CreateObject("ADODB.Command")
      Set adoConnection = CreateObject("ADODB.Connection")
        adoConnection.Provider = "ADsDSOObject"
        adoConnection.Open "Active Directory Provider"
        ad.ActiveConnection = adoConnection
    
    'Put the distinguishedname of your two groups here:
    strFilter = "(|(memberof=CN=Group Name,OU=....)(memberof=CN=Group Name 2,OU=....))"
    
    'Chose what you want to return here:
    strAttributes = "samaccountname,cn"
    
    strQuery = "<LDAP://" & strDNSDomain & ">" & ";" & strFilter & ";" & strAttributes & ";subtree"
    
      ad.CommandText = strQuery
      ad.Properties("SearchScope") = 2 
      ad.Properties("Page Size") = 1000
      ad.Properties("Cache Results") = False
    Set objRS = ad.Execute
    

    现在您已经在一个记录集中获得了所有结果,您可以按照自己的方式处理它们,将每个结果写入一个文件或任何您想做的事情。所以像:

    Do Until objRS.EOF
    
    
       'Do something with each value
       objRS.Fields("samaccountname")
    
    objRS.MoveNext
    Loop
    

    有用吗?我在这里假设您对编写 LDAP 查询有所了解

    【讨论】:

      【解决方案2】:

      查找 Active Directory 脚本的最佳位置是 Microsoft 的脚本中心 Repository

      您可以找到列出所有组和所有组成员here 的脚本(“列出域中的所有组和组的所有成员”)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 1970-01-01
        相关资源
        最近更新 更多