Re: Get SAMAccountNames for all users in an active directory group



Yep, this is a good use for attribute scope query. There is a .NET code
sample from ch. 5 of our book which can be downloaded from our book's
website (see link below).

Most of the time, in situations where you could to an ASQ, you can simply do
the search "backwards". For example, you can search for all objects that
have memberOf = the DN of the group in question and return the
sAMAccountName and this will do the same basic thing as an ASQ would.

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
"Dean Wells (MVP)" <dwells@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:%23H2SWgsWIHA.3400@xxxxxxxxxxxxxxxxxxxxxxx
Do some digging on an LDAP control/feature called 'attribute scoped
queries' ... this will do as you ask in a single (from the client-side at
least) query.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R a m o v e t h e m a s k t o s e n d e m a i l


<psychrodraconic@xxxxxxxxx> wrote in message
news:c8a43f19-50d0-48ee-b655-58f14e1f5e98@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I am trying to get a list of NT Login Names (SAMAccountNames) for the
users that belong to an active directory group. I have a function
that is used for receiving a list of the members of a group. This
works perfectly for getting the members. However, not so much for
getting the SAMAccountNames. Could anyone give me any guidance on how
to get the account names for a group in active directory?

Below is a sample method for getting the members for that group.

Public Shared Function getGroupMembers(ByVal group As String)
As ArrayList

Dim Members As ArrayList = New ArrayList()
Dim _path As String =
ConfigurationManager.AppSettings("ADConnectionString")

'------ Start Impersonation ------
Dim ImpersonateContext As WindowsImpersonationContext =
Utilities.Impersonate(LDAPuser, LDAPpassword, LDAPdomain)

'------ Start running code -------
Dim entry As DirectoryEntry = New DirectoryEntry(_path,
LDAPdomain + "\" + LDAPuser, LDAPpassword,
AuthenticationTypes.ServerBind)


Dim search As DirectorySearcher = New
DirectorySearcher(entry)

search.Filter = "(cn=" & group & ")"
search.PropertiesToLoad.Add("member")
Dim result As SearchResult = search.FindOne()

If Not (result Is Nothing) Then

Dim propertyCount As Integer =
result.Properties("member").Count

Dim equalsIndex, commaIndex As Integer
Dim user As String

For counter As Integer = 0 To propertyCount - 1

user = result.Properties("member")
(counter).ToString

equalsIndex = user.IndexOf("=", 1)
commaIndex = user.IndexOf(",", 1)

If Not (equalsIndex = -1) Then

Members.Add(user.Substring((equalsIndex + 1),
(commaIndex - equalsIndex) - 1))

End If

Next

End If

'------ End Impersonation ------
ImpersonateContext.Undo()

Members.Sort()

Return Members

End Function





.



Relevant Pages

  • Re: Another Scripting Newbie - admin check
    ... members, members due to group nesting of local groups, members of domain ... Dim objRootDSE, strDNSDomain, adoConnection, adoCommand ... ' Bind to the local Administrators group on each computer ... For Each objMember In objLocalGroup.Members ...
    (microsoft.public.windows.server.scripting)
  • RE: Making a collection of EntireRows for copying and then deletin
    ... Dim rngToSearch As Range ... Set rngFoundAll = Union ... ' The rest of the code finds the correct start cell in the archive file; ... I have a small data base (30-40 members of a local club). ...
    (microsoft.public.excel.programming)
  • Re: Function works in WidowsXP but not Windows 2000
    ... Seems as the only way to create a distribution list is to have the members ... folder and stores those names and emails so that distribution lists of 50 ... Dim Cntct As String, DistListName As String ... Dim Groupsize as Integer ...
    (microsoft.public.office.developer.outlook.vba)
  • Re: List Members of a Group (with user input)
    ... Wscript.Echo "No members" ... Dim objRootDSE, objTrans, strNetBIOSDomain, strGroup ... I would like one that does not require editing of the script each time. ... Dim UserCount, gga, CNgga ...
    (microsoft.public.scripting.vbscript)
  • Re: Cannot make distribution list without resolving to Contacts Folder
    ... something about apparently indicates to Outlook that it is really a Contact. ... "Could not find Contact 'dmitry'. ... >> 'once all the members have been added, ... >>> Dim conn As ADODB.Connection ...
    (microsoft.public.outlook.program_vba)