Link to home
Start Free TrialLog in
Avatar of Javier_G
Javier_G

asked on

List Groups in AD using vbscript

Is there a way to list all groups in Active Directory using vbscript? More specifically Distribution Groups?
Avatar of Shane Russell
Shane Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Javier_G
Javier_G

ASKER

I believe this would display local groups not Active Directory Groups.
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
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.
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.
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 ?
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.

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<><>
I had already answered the question myself. I would like the points refunded.
Please post your solution. Otherwise I will recommend no points refunded for ignoring the last expert comment.
I already posted it on Date: 04/29/2005 03:36PM PDT, it starts out with "Here it is:". Read above.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial