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

Jerold Schulman

October 29, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today

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 NextDim objConnection, objCommand, objRootDSE, strDNSDomainDim strFilter, strQuery, objRecordSet, gtSet objConnection = CreateObject("ADODB.Connection")Set objCommand = CreateObject("ADODB.Command")objConnection.Provider = "ADsDSOOBject"objConnection.Open "Active Directory Provider"Set objCommand.ActiveConnection = objConnectionSet objRootDSE = GetObject("LDAP://RootDSE")'Get domainstrDNSDomain = objRootDSE.Get("defaultNamingContext")strBase = ""'Define the filter elementsstrFilter = "(&(objectCategory=group))"'List all attributes you will requirestrAttributes = "distinguishedName,sAMAccountName,groupType"'compose querystrQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"objCommand.CommandText = strQueryobjCommand.Properties("Page Size") = 99999objCommand.Properties("Timeout") = 300objCommand.Properties("Cache Results") = FalseSet objRecordSet = objCommand.ExecuteobjRecordSet.MoveFirstDo 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.MoveNextLoop' Clean up.objConnection.CloseSet objConnection = NothingSet objCommand = NothingSet objRootDSE = NothingSet objRecordSet = Nothing


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like