Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Remove All Group Memberships for all User Accounts in an OU?

Reply
Thread Tools Display Modes

Remove All Group Memberships for all User Accounts in an OU?

 
 
Guest
Posts: n/a

 
      09-17-2008
I've seen the examples with

LDAP://cn=MyerKen,ou=Management,dc=NA,dc=fabrikam,dc=com

but it's only one account, I have a whole OU full of accounts whose group
memberships I need to clear (Domain Users excepted of course). I know I
can't modify the user account I have to modify the group, because memberof
is backlinked, but is there a way to essentially loop through the actions of
the one account and its group memberships, then move to the next user
account and repeat?

Any help appreciated


 
Reply With Quote
 
 
 
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      09-17-2008

<-> wrote in message news:...
> I've seen the examples with
>
> LDAP://cn=MyerKen,ou=Management,dc=NA,dc=fabrikam,dc=com
>
> but it's only one account, I have a whole OU full of accounts whose group
> memberships I need to clear (Domain Users excepted of course). I know I
> can't modify the user account I have to modify the group, because memberof
> is backlinked, but is there a way to essentially loop through the actions
> of the one account and its group memberships, then move to the next user
> account and repeat?
>
> Any help appreciated


You can enumerate all users in the OU, and for each user enumerate all
direct group memberships, which will not include the "primary" group (which
should be "Domain Users"). You can use the Remove method of each group
object to remove the user from the group. To save binding to each group
repeatedly for many users, I would track the groups in a dictionary object.
For example (not tested):
============
Option Explicit

Dim objOU, objUser, arrGroups, strGroup, objGroup
Dim objGroupList

' Bind to OU object.
Set objOU = GetObject("LDAP://ou=West,dc=MyDomain,dc=com")

' Filter on objects of class user.
objOU.Filter = Array("user")

' Create dictionary object of group objects.
Set objGroupList = CreateObject("Scripting.Dictionary")
objGroupList.CompareMode = vbTextCompare

' Enumerate users in OU.
For Each objUser In objOU
' Enumerate direct group memberships.
' Trap error if there are no groups.
' Primary group is not included.
On Error Resume Next
arrGroups = objUser.GetEx("memberOf")
If (Err.Number = 0) Then
On Error GoTo 0
For Each strGroup In arrGroups
' Check if group already bound.
If (objGroupList.Exists(strGroup) = False) Then
' Add group object to the dictionary object.
Set objGroupList(strGroup) = GetObject("LDAP://" & strGroup)
End If
' Remove user from the group.
objGroupList(strGroup).Remove(objUser.AdsPath)
Next
End If
On Error GoTo 0
Next

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Add User to group through comparison to other user's memberships Ace Fekay [Microsoft Certified Trainer] Active Directory 0 03-31-2009 07:09 AM
Re: Add User to group through comparison to other user's memberships Richard Mueller [MVP] Active Directory 4 03-28-2009 04:15 AM
max number of group memberships of a user? tree leafs Active Directory 9 01-21-2008 12:29 AM
Need CSVDE to dump user group memberships Spin Active Directory 4 01-18-2006 01:56 AM
Need CSVDE to dump user group memberships Spin Scripting 4 01-18-2006 01:56 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59