Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


October 30, 2006

How can I use VBScript to return all the groups in my domain, returning distinguishedName, sAMAccountName, group scope and type in CSV format?

RSS
Subscribe to Windows IT Pro | See More Domains Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

10945 » How can I use VBScript to return all the groups in my domain, returning distinguishedName, sAMAccountName, group scope and type in CSV format? (30-Oct-06)

I have scripted GetGroups.vbs to return all the groups in your domain, returning the following in semi-colon separated format:

"distinguishedName";"sAMAccountName";"Scope";"SecDst"

Where:

DistinguishedName is the distinguished name of the group.

sAMAccountName    is the NetBIOS (Pre-Windows 2000) name of the group.


Scope             is the group scope:
                         B   for a Built-in group.
                         L   for a Domain Local group.
                         G   for a Global group.
                         U   for a Universal group.

SecDst            is the group type:
                         S   for a security group.
                         D   for a distribution group.
GetGroups.vbs contains:
On Error Resume Next
Dim objConnection, objCommand, objRootDSE, strDNSDomain
Dim strFilter, strQuery, objRecordSet, gt
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
'Get domain
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
'Define the filter elements
strFilter = "(&(objectCategory=group))"
'List all attributes you will require
strAttributes = "distinguishedName,sAMAccountName,groupType"
'compose query
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 99999
objCommand.Properties("Timeout") = 300
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    strDN = objRecordSet.Fields("distinguishedName")
    strSA = objRecordSet.Fields("sAMAccountName")
    gt = objRecordSet.Fields("groupType")
    if (gt ANd &h01) <> 0 then
    Scope = "B"
    ElseIf (gt And &h02) <> 0 Then
    Scope = "G"
    ElseIf (gt And &h04) <> 0 Then
    Scope = "L"
    ElseIf (gt And &h08) <> 0 Then
    Scope = "U"
    End If
    If (gt And &h80000000) <> 0 Then
     SecDst = "S"
    Else
     SecDst = "D"
    End If
    Wscript.Echo """" & strDN & """;""" & strSA & """;""" & Scope & """;""" & SecDst &""""
    objRecordSet.MoveNext
Loop
' Clean up.
objConnection.Close
Set objConnection = Nothing
Set objCommand = Nothing
Set objRootDSE = Nothing
Set objRecordSet = Nothing


End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 24, 2008

An often irreverent look at some of the week's other news, including a Vista Capable dismissal request, Zune price reductions, Morrow musings, Novell and Microsoft sitting in a tree ... two years later, Yahoo!, IE 6 on Windows Mobile, and so much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...


Related Events Check out our list of Free Email Newsletters!

Related Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing