Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

List Groups in AD using vbscript

Posted on 2005-04-29
15
Medium Priority
?
7,217 Views
Last Modified: 2010-09-28
Is there a way to list all groups in Active Directory using vbscript? More specifically Distribution Groups?
0
Comment
Question by:Javier_G
13 Comments
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13897575
What about this :

' Get the name of the computer
str_Computer = WScript.CreateObject("WScript.Network").ComputerName

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oOutFile = oFSO.CreateTextFile("c:\usersandgroups.txt")

' Connect to the computer object
set oMachine = getObject("WinNT://" & str_Computer & ",computer")

' Set the filter to list users
oMachine.Filter = Array("group")

' For each group on the machine
For Each oGroup in oMachine
  oOutFile.writeline oGroup.Name

  ' For each user in the group
  For Each oUser in oGroup.Members
    ' display the user name
    oOutFile.writeline  vbTab & oUser.Name
  Next
Next

oOutFile.Close

Set oOutFile = Nothing
Set oFSO = Nothing
0
 

Author Comment

by:Javier_G
ID: 13897638
I believe this would display local groups not Active Directory Groups.
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13897783
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13897793
More precisely the site from the above one to do what you want is :

http://cwashington.netreach.net/depo/view.asp?Index=1037&ScriptType=vbscript
0
 

Author Comment

by:Javier_G
ID: 13898435
I thought we were there.....That script works great but it displays the exact opposite of what I wanted. It only lists security groups, what I need is distribution groups.
0
 

Author Comment

by:Javier_G
ID: 13898657
Here it is:

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
    "SELECT ADsPath FROM 'LDAP://dc=domain,dc=com' WHERE objectCategory='group' "
   
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    Set objType = GetObject(objRecordSet.Fields("ADsPath").Value)
        If objType.GroupType > 0  Then
            Wscript.Echo "Group: " & objType.cn
        End If
    objRecordSet.MoveNext
Loop

If you make the GroupType < 0 then it will display only security groups.
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13899083
what are you doing with this question then, also did the URL I posted help in anyway what so ever or you just going to close this question out now ?
0
 

Author Comment

by:Javier_G
ID: 13911207
What you had posted was not what I needed. In my initial question I was asking for enumerating specifically distribution groups.

I would like this question to be closed now.

0
 
LVL 15

Expert Comment

by:sr75
ID: 13913431
You can close it after you test my script.  Of course you will need to edit it to reflect you domain.

<><>Begin Script<><>

On Error Resume Next

Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
Const ADS_GROUP_TYPE_LOCAL_GROUP = &h4
Const ADS_GROUP_TYPE_UNIVERSAL_GROUP = &h8
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000

set DomainOU = GetObject("LDAP://cn=users,dc=domain,dc=COM")

DomainOU.Filter = Array("group")

For each objGroup in DomainOU
      strGroup = objGroup.cn
      set objGroup = GetObject("LDAP://cn=" & strGroup & "cn=users,dc=domain,dc=COM")
      objGroup.GetInfo
      intGroupType = objGroup.Get("groupType")

      If intGroupType AND ADS_GROUP_TYPE_SECURITY_ENABLED then
            List = List
      else
              List = List & strGroup & vbtab & "Distribution group" & vbCrLf
      End If
next

wscript.echo List

<><>end script<><>
0
 

Author Comment

by:Javier_G
ID: 14188048
I had already answered the question myself. I would like the points refunded.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 14238883
Please post your solution. Otherwise I will recommend no points refunded for ignoring the last expert comment.
0
 

Author Comment

by:Javier_G
ID: 14240862
I already posted it on Date: 04/29/2005 03:36PM PDT, it starts out with "Here it is:". Read above.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 14345101
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Sometimes it necessary to set special permissions on user objects.  For instance when using a Blackberry server, the SendAs permission needs to be set. I see many admins struggle with the setting that permission only to see it disappear within a few…
Simple Linear Regression
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

580 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