Solved

Querying Active Directory group membership information

Posted on 2013-11-30
6
942 Views
Last Modified: 2014-01-14
Good day experts,

I'm trying to query active directory with vbscript to get the membership information of a specific group.

Using an application called dsa.msc I can query this information manually but I need to follow a more automated approach using vbscript.

SO what I need my script to do now, is get all the members for a given group name.

I currently have the following sample:

Set objGroup = GetObject _
  ("LDAP:// SOMETHING MUST GO HERE")
objGroup.GetInfo
 
arrMemberOf = objGroup.GetEx("member")
 
WScript.Echo "Members:"
For Each strMember in arrMemberOf
    WScript.echo strMember
Next

I just cannot get the correct parameters specified in the "LDAP:// ..... " command to get results.  Surely, if I can access the required info with dsa.msc then I should have all the necessary connectivity, access and info to run this query - I just don't know what to put in.

Please help.

Thanks
Christoff
0
Comment
Question by:PantoffelSlippers
6 Comments
 
LVL 53

Accepted Solution

by:
Will Szymkowski earned 250 total points
ID: 39686989
Powershell is much easier. You can use 1 line of code to accomplish what you are doing... Use the following command below...

Security Group (Powershell with import-module activedirectory)
Get-ADGroupMember -Identity <groupname> | select name,samaccountname


Distribution Group (run from Exchange Management Shell)
Get-DistributionGroupMember -Identity <groupname> | Select DisplayName


Will.
0
 
LVL 14

Assisted Solution

by:Ram Balachandran
Ram Balachandran earned 150 total points
ID: 39687199
Following is a sample vbscript to find members of a AD group
For your question : You need to provide DN Name of the group

Set objGroup = GetObject ("LDAP://cn=Test_Group,ou=TestOU,dc=home,dc=com")

Say Test_Group is your group name - In dsa.msc, in view > select advanced features
and then search for the group.
Open group properties and select Object

You can see the AD path of the group like home.com/SomeOU/Groupname
Now home.com (or your domain name will become Dc=home,Dc=com.


Sample Code

' List All the 'Members' and 'Member Of' of a Group
' Test_group points to the group which you need the output

On Error Resume Next

Set objGroup = GetObject ("LDAP://cn=Test_Group,ou=TestOU,dc=home,dc=com")
objGroup.GetInfo

arrMember = objGroup.GetEx("member")

WScript.Echo "Members:"
For Each strMember in arrMember
    WScript.echo strMember
Next
'------------------------------

arrMemberOf = objGroup.GetEx("memberof")
WScript.Echo "MemberOf:"
For Each strMemberOf in arrMemberOf
    WScript.echo strMemberOf
Next
'------------------------------

Open in new window


--------------------


Using Powershell first import Active directory module

http://blogs.msdn.com/b/rkramesh/archive/2012/01/17/how-to-add-active-directory-module-in-powershell-in-windows-7.aspx

Get-ADGroupMember -Identity Test_Groupname | select name,samaccountname

Replace Test_Groupname with your group
0
 

Author Comment

by:PantoffelSlippers
ID: 39687831
Thanks - let me try this out
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 9

Expert Comment

by:VirastaR
ID: 39688271
Hi,

Check this too..

[SOLVED] Active Directory Saved Query - Group Member Listing

Hope that helps :)
0
 

Author Comment

by:PantoffelSlippers
ID: 39729626
I'll try that too.

I'm really struggling with the Powershell.

One of my challenges is that results are written back to a database - that's why I also started with VBScript - easy to update SQL Server...
0
 

Author Closing Comment

by:PantoffelSlippers
ID: 39778646
Thanks experts
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now