Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: Script AD remove all members groups OU

Reply
Thread Tools Display Modes

Re: Script AD remove all members groups OU

 
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      03-18-2009

"helena_carvalho" <u50449@uwe> wrote in message news:93393962db97b@uwe...
> Hi,
>
> I have more 5000 security groups in AD, and i need a script that remove
> all
> members to all groups in specified OU. can hep me.
>
> thanks.
> helena carvalho
>


If you want to remove all members of a group that are in a specified OU, you
can enumerate the direct members of the group, retrieve the DN of the parent
container/OU, compare this to the DN of the specified OU, then remove
members whose parent matches. For example:
=======
' Specify Distinguished Name of OU. All users in this OU
' that are members of the specified group will be removed.
strOU = "ou=Sales,ou=West,dc=MyDomain,dc=com"

' Bind to the specified group.
Set objGroup = GetObject("LDAP://cn=TestGroup,ou=West,dc=MyDomain,dc=com")

' Enumerate all direct members of the group.
For Each objMember In objGroup.
' Retrieve DN of parent container/OU of member.
Set objParent = GetObject(objMember.Parent)
strParentDN = objParent.distinguishedName
' Compare to specified OU.
If (LCase(strParentDN) = LCase(strOU)) Then
' Remove the member from the group.
objGroup.Remove(objMember.AdsPath)
End If
Next

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


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

 
      03-18-2009
There needs to be a way to identify the groups. If you mean all groups in a
specified OU you could enumerate them with code similar to:
====
' Specify Distinguished Name of OU. All users in this OU
' that are members of the specified group will be removed.
strOU = "ou=Sales,ou=West,dc=MyDomain,dc=com"

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

' Filter on group objects.
objOU.Filter = Array("group")

' Enumerate all groups in the OU.
For Each objGroup In objOU
' Enumerate all direct members of the group.
For Each objMember In objGroup.Members
' Retrieve DN of parent container/OU of member.
Set objParent = GetObject(objMember.Parent)
strParentDN = objParent.distinguishedName
' Compare to specified OU.
If (LCase(strParentDN) = LCase(strOU)) Then
' Remove the member from the group.
objGroup.Remove(objMember.AdsPath)
End If
Next
Next
========
Otherwise, perhaps you can read group DN's from a text file.

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

"helena_carvalho via WinServerKB.com" <u50449@uwe> wrote in message
news:9343d16a2e44a@uwe...
> This script works when we have a few groups , I have 5000 groups.
> There are a script do it.
>
> thanks
>
> Richard Mueller [MVP] wrote:
>>> Hi,
>>>

>>[quoted text clipped - 4 lines]
>>> thanks.
>>> helena carvalho

>>
>>If you want to remove all members of a group that are in a specified OU,
>>you
>>can enumerate the direct members of the group, retrieve the DN of the
>>parent
>>container/OU, compare this to the DN of the specified OU, then remove
>>members whose parent matches. For example:
>>=======
>>' Specify Distinguished Name of OU. All users in this OU
>>' that are members of the specified group will be removed.
>>strOU = "ou=Sales,ou=West,dc=MyDomain,dc=com"
>>
>>' Bind to the specified group.
>>Set objGroup = GetObject("LDAP://cn=TestGroup,ou=West,dc=MyDomain,dc=com")
>>
>>' Enumerate all direct members of the group.
>>For Each objMember In objGroup.
>> ' Retrieve DN of parent container/OU of member.
>> Set objParent = GetObject(objMember.Parent)
>> strParentDN = objParent.distinguishedName
>> ' Compare to specified OU.
>> If (LCase(strParentDN) = LCase(strOU)) Then
>> ' Remove the member from the group.
>> objGroup.Remove(objMember.AdsPath)
>> End If
>>Next
>>

>
> --
> Message posted via WinServerKB.com
> http://www.winserverkb.com/Uwe/Forum...pting/200903/1
>



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

 
      03-19-2009
If you have a file of group Distinguished Names (DN's), the code could be
similar to below:
=========
Const ForReading = 1

' Specify file of group Distinguished Names.
strFile = "c:\scripts\groups.txt"

' Specify Distinguished Name of OU. All users in this OU
' that are members of the any of the groups will be removed.
strOU = "ou=Sales,ou=West,dc=MyDomain,dc=com"

' Open the file for read access.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)

' Read the file.
Do Until objFile.AtEndOfStream
' Retrieve group DN.
strGroupDN = Trim(objFile.ReadLine)
' Skip blank lines.
If (strGroupDN <> "") Then
' Bind to the group.
Set objGroup = GetObject("LDAP://" & strGroupDN)
' Enumerate all direct members of the group.
For Each objMember In objGroup.Members
' Retrieve DN of parent container/OU of member.
Set objParent = GetObject(objMember.Parent)
strParentDN = objParent.distinguishedName
' Compare to specified OU.
If (LCase(strParentDN) = LCase(strOU)) Then
' Remove the member from the group.
objGroup.Remove(objMember.AdsPath)
End If
Next
End If
Loop

' Clean up.
objFile.Close
=========

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

"Richard Mueller [MVP]" <rlmueller-> wrote in
message news:%238G%...
> There needs to be a way to identify the groups. If you mean all groups in
> a specified OU you could enumerate them with code similar to:
> ====
> ' Specify Distinguished Name of OU. All users in this OU
> ' that are members of the specified group will be removed.
> strOU = "ou=Sales,ou=West,dc=MyDomain,dc=com"
>
> ' Bind to specified OU.
> Set objOU = GetObject("LDAP://ou=Sales,ou=West,dc=MyDomain,dc=com")
>
> ' Filter on group objects.
> objOU.Filter = Array("group")
>
> ' Enumerate all groups in the OU.
> For Each objGroup In objOU
> ' Enumerate all direct members of the group.
> For Each objMember In objGroup.Members
> ' Retrieve DN of parent container/OU of member.
> Set objParent = GetObject(objMember.Parent)
> strParentDN = objParent.distinguishedName
> ' Compare to specified OU.
> If (LCase(strParentDN) = LCase(strOU)) Then
> ' Remove the member from the group.
> objGroup.Remove(objMember.AdsPath)
> End If
> Next
> Next
> ========
> Otherwise, perhaps you can read group DN's from a text file.
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
> "helena_carvalho via WinServerKB.com" <u50449@uwe> wrote in message
> news:9343d16a2e44a@uwe...
>> This script works when we have a few groups , I have 5000 groups.
>> There are a script do it.
>>
>> thanks
>>
>> Richard Mueller [MVP] wrote:
>>>> Hi,
>>>>
>>>[quoted text clipped - 4 lines]
>>>> thanks.
>>>> helena carvalho
>>>
>>>If you want to remove all members of a group that are in a specified OU,
>>>you
>>>can enumerate the direct members of the group, retrieve the DN of the
>>>parent
>>>container/OU, compare this to the DN of the specified OU, then remove
>>>members whose parent matches. For example:
>>>=======
>>>' Specify Distinguished Name of OU. All users in this OU
>>>' that are members of the specified group will be removed.
>>>strOU = "ou=Sales,ou=West,dc=MyDomain,dc=com"
>>>
>>>' Bind to the specified group.
>>>Set objGroup =
>>>GetObject("LDAP://cn=TestGroup,ou=West,dc=MyDomain,dc=com")
>>>
>>>' Enumerate all direct members of the group.
>>>For Each objMember In objGroup.
>>> ' Retrieve DN of parent container/OU of member.
>>> Set objParent = GetObject(objMember.Parent)
>>> strParentDN = objParent.distinguishedName
>>> ' Compare to specified OU.
>>> If (LCase(strParentDN) = LCase(strOU)) Then
>>> ' Remove the member from the group.
>>> objGroup.Remove(objMember.AdsPath)
>>> End If
>>>Next
>>>

>>
>> --
>> Message posted via WinServerKB.com
>> http://www.winserverkb.com/Uwe/Forum...pting/200903/1
>>

>
>



 
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
Script to remove user from All Groups in AD gbrown135 Scripting 1 06-13-2007 02:05 PM
Delete Members of groups from a script gbrown135 Scripting 2 06-05-2007 10:03 AM
list all groups and members of those groups Villain Scripting 4 05-19-2006 12:28 PM
Script fails to remove migrated accounts from local groups Michael Walleisa Active Directory 0 06-01-2005 05:20 PM
Adding Groups as members of Groups ??? Optikal Active Directory 9 10-27-2004 09:10 PM



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