【问题标题】:Removing Active Directory User from Groups where Group Name Starts With从组名开头的组中删除 Active Directory 用户
【发布时间】:2018-01-31 11:38:23
【问题描述】:

我在尝试解决 VB.net 中的问题时遇到了麻烦。我想要实现的是从组名称以“Google”开头的所有组中删除一个特定的 AD 用户...

如果我知道组的全名,这很简单,我可以执行以下操作:

Dim ctx As DirectoryServices.AccountManagement.PrincipalContext = New DirectoryServices.AccountManagement.PrincipalContext(DirectoryServices.AccountManagement.ContextType.Domain, "Company.co.uk")
Dim googleremove As DirectoryServices.AccountManagement.GroupPrincipal = DirectoryServices.AccountManagement.GroupPrincipal.FindByIdentity(ctx, "Google-Group1")
googleremove.Members.Remove(ctx, DirectoryServices.AccountManagement.IdentityType.SamAccountName, "UserID")
googleremove.Save()

但问题是我的应用程序并不总是知道需要从哪个特定组中删除用户。有 28 个不同的组,每个组都有数千名用户,组名以“Google-”开头。有没有一种有效的方法可以将用户从组名以“Google-”开头的所有组中删除,这样不会严重减慢速度?

【问题讨论】:

  • 如果您只获取用户所属的组,然后将他从所有以“Google”开头的组中删除,该怎么办。根据组成员的数量,这可能会快很多......?
  • @MatSnow 是的,这很合适。我可以使用 System.DirectoryServices 获取 MemberOf 信息,但我不知道如何获得更多信息......
  • 我不知道该怎么做 -> 我不明白...您已经发布了从组中删除用户的代码。有什么问题?
  • @MatSnow 我的问题是我可以孤立地做每件事,但不具备将这两个过程结合起来的知识。

标签: vb.net active-directory directoryservices account-management


【解决方案1】:

我解决了!以下是我为其他人解决我的问题的方法:

Dim ctx As DirectoryServices.AccountManagement.PrincipalContext = New DirectoryServices.AccountManagement.PrincipalContext(DirectoryServices.AccountManagement.ContextType.Domain, "MyCompany.co.uk")
Dim usr As DirectoryServices.AccountManagement.UserPrincipal = DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(ctx, "User ID")
Dim grp As DirectoryServices.AccountManagement.GroupPrincipal = New DirectoryServices.AccountManagement.GroupPrincipal(ctx)
grp.Name = "Google-*"
grp.Members.Contains(usr)
Dim srch As DirectoryServices.AccountManagement.PrincipalSearcher = New DirectoryServices.AccountManagement.PrincipalSearcher(grp)
For Each s As DirectoryServices.AccountManagement.GroupPrincipal In srch.FindAll()
    s.Members.Remove(ctx, DirectoryServices.AccountManagement.IdentityType.SamAccountName, "User ID")
    s.Save()
Next

【讨论】:

    【解决方案2】:

    您说您知道如何获取 MemberOf 信息。您是否会遍历该数组以查找以“Google”开头的组。

    但请记住,MemberOf 数组是一组 distinctNames,因此组名以“CN=”为前缀。所以你真的需要做这样的事情:

    For Each groupDn as String in memberOf
        If groupDn.StartsWith("CN=Google"))
            //remove user from this group
        End If
    Next
    

    我有一段时间没有使用 VB,所以它可能无法按原样工作。但这就是想法。

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多