Question : I need to query each group in an OU in the Root Domain and get the group names that has exact identical users.

Hi,

I need to query each group in an OU in the Root Domain and get the group names that has exact identical users.
get output as

Group1 has 10 members and they are identical
Group2 has 10 members and they are identical

Need to check the member email id's could be user accounts or contacts in the groups

Regards
Sharath

Answer : I need to query each group in an OU in the Root Domain and get the group names that has exact identical users.

check this code:
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:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
Set oObject = GetObject("LDAP://ad2008rd/cn=Users,DC=soap,DC=com")
oObject.Filter= Array("Group")

ReDim arrGroups(1)
ReDim arrGroupMembers(1)
Dim oGroup,fileName,groupName,arrGroupMembersCounter,emptyGroupsCounter,groupsCounter

arrGroupMembersCounter=0
emptyGroupsCounter=0
groupsCounter=0

For Each oGroup in oObject
	dim members
	members=0
	
	groupName = oGroup.cn
	
	ReDim preserve arrGroups(groupsCounter+1)

	groupPath = getgrouppath(groupName)

	if groupPath = "" then
		wscript.echo "Unable to find the specified group in the domain"
	else
		set objGroup = getobject(grouppath)
	
		for each objMember in objGroup.Members
			members=members+1
			if arrGroupMembers(arrGroupMembersCounter) = "" then
				arrGroupMembers(arrGroupMembersCounter) = objmember.samaccountname
			else
				arrGroupMembers(arrGroupMembersCounter) = arrGroupMembers(arrGroupMembersCounter) & "," & objmember.samaccountname
			end if 			
		next

		getprimarygroupmembers groupname

		wscript.echo groupName & ":" 
		if arrGroupMembers(arrGroupMembersCounter) = "" then
			emptyGroupsCounter=emptyGroupsCounter+1
			wscript.echo "EMPTY GROUP..."
		else
			arrGroups(groupsCounter)=groupName
			groupsCounter=groupsCounter+1

			wscript.echo "Number of Members: " & members
			wscript.echo arrGroupMembers(arrGroupMembersCounter)
			arrGroupMembersCounter=arrGroupMembersCounter+1
			ReDim preserve arrGroupMembers(arrGroupMembersCounter+1)
		end if
				
		wscript.echo ""
		end if
Next

wscript.echo "Number of groups: " & groupsCounter
wscript.echo "Number of empty groups: " & emptyGroupsCounter

findDuplicateGroups()

wscript.echo "Completed"
		
function findDuplicateGroups()
	wscript.echo ""
	wscript.echo "find duplicate groups members:"

	for i = 0 to ubound(arrGroups)
		for j = 0 to ubound(arrGroups)
			if i<>j then
				if arrGroupMembers(i) = arrGroupMembers(j) then
					wscript.echo arrGroups(i) & " and " & arrGroups(j) & " has the same members:"
					wscript.echo arrGroupMembers(j) 
					wscript.echo ""
				end if
			end if
		next
	next 
end function

function getGroupPath(byval GroupName)
	set cmd=createobject("ADODB.Command")
	set cn=createobject("ADODB.Connection")
	set rs=createobject("ADODB.Recordset")
	
	cn.open "Provider=ADsDSOObject;"
	
	cmd.commandtext = "SELECT adspath from 'LDAP://" & getnc & _
			  "' WHERE objectCategory = 'Group' and sAMAccountName = '" & groupname & "'"
	cmd.activeconnection = cn
	
	set rs = cmd.execute
	
	if rs.bof <> true and rs.eof<>true then
		getgrouppath=rs(0)
	else
		getgrouppath = ""
	end if
	cn.close

end function

function getNC
	set objRoot=getobject("LDAP://RootDSE")
	getNC=objRoot.get("defaultNamingContext")
end function

function getPrimaryGroupMembers(byval GroupName)
	set cn = createobject("ADODB.Connection")
	set cmd = createobject("ADODB.Command")
	set rs = createobject("ADODB.Recordset")
	
	cn.open "Provider=ADsDSOObject;"
	cmd.activeconnection=cn

	cmd.properties("page size")=1000
	cmd.commandtext = "SELECT PrimaryGroupToken FROM 'LDAP://" & getnc & _
			  "' WHERE sAMAccountName = '" & GroupName & "'"
	set rs = cmd.execute

	if rs.eof<>true and rs.bof<>true then
		PrimaryGroupID = rs(0)
	else
		Err.Raise 5000, "getPrimaryGroupMembers", "Unable to find PrimaryGroupToken property"
	end if

	cmd.commandtext = "SELECT samaccountname, sn, givenName FROM 'LDAP://" & getNC & _
			  "' WHERE PrimaryGroupID = '" & PrimaryGroupID & "'"

	set rs = cmd.execute

	while rs.eof<>true and rs.bof<>true
		if arrGroupMembers(arrGroupMembersCounter) = "" then
			arrGroupMembers(arrGroupMembersCounter) = rs("samaccountname")
		else
			arrGroupMembers(arrGroupMembersCounter) = arrGroupMembers(arrGroupMembersCounter) & "," & rs("samaccountname")
		end if 	
			
		rs.movenext
	wend
	cn.close
	
end function
Random Solutions  
 
programming4us programming4us