February 22 2011

Copy AD group membership from one group to another

Here is a good VB script for copying the group membership from one Active Directory group to another:
strSGroupDN = InputBox ("Enter the DN of Source Group" & VBCRLF &_
vbcrlf& _
vbcrlf& _
"e.g. CN=Source Group,OU=Users,DC=NWTraders,DC=com")
strDGroupDN = InputBox ("Enter the DN of Destination Group" & VBCRLF &_
vbcrlf& _
vbcrlf& _
"e.g. CN=Destination Group,OU=Users,DC=NWTraders,DC=com")
set dicSeenGroupMember = CreateObject("Scripting.Dictionary")
set objDGroup = GetObject("LDAP://" & strDGroupDN)
DisplayMembers "LDAP://" & strSGroupDN, dicSeenGroupMember
Function DisplayMembers (strGroupADsPath, dicSeenGroupMember)
set objGroup = GetObject(strGroupADsPath)
for each objMember In objGroup.Members
objDGroup.Add("LDAP://" & objMember.distinguishedName)
End Function
MsgBox "Group Members have been copied to Destination Group"

Quotation marks are not required when entering values into the input boxes, even if your DN has spaces in it.

I use a maximum of one Google Ad per post to help offset some of my blog hosting costs.


March 31 2010

Query to show members of an AD group

You’ll often need a quick way to get the names of users in a particular Active Directory group into a text or Excel file for reporting or other reasons. You can use dsquery to achieve this easily:

dsquery group -name "group name" | dsget group -members -expand | dsget user -fn -ln

Of course you can also change the ‘dsget user -fn -ln’ to display or remove attributes, eg ‘dsget user -samid’ will show the username. You can also pipe this into a text file.