473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Enumerate members of Administrators Group (AD)

Hi,

I found this handy script and I'm trying to convert it to VB.NET. It
pops up a list of members in the Administrators/Builtin group. Can't
seem to get DirectorySearcher or DirectoryEntry working similar to
this. Run this as a VBS script to see:

Option Explicit
Dim strUser, strMember, strDNSDomain, strContainer
Dim objGroup, objUser, objRootDSE
Dim arrMemberOf

' Bind to Active Directory'
strContainer = "cn=Administrators,cn=Builtin, "
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

' Get the Builtin Administrators group
Set objGroup = GetObject ("LDAP://"& strContainer & strDNSDomain)
objGroup.getInfo

arrMemberOf = objGroup.GetEx("member")

' Loop = For Each .... Next
WScript.Echo "Members of Group " & strContainer
For Each strMember in arrMemberOf
WScript.echo strMember
Next

Wscript.Quit

Thanks for your help!

Mar 2 '06 #1
23 12314
Imports System.DirectoryServices
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports ActiveDs

Private Sub cmdQuery_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdQuery.Click
Dim objMembers As Object = Nothing
Dim collMembers As IADsMembers = Nothing
Dim iadsMember As IADsUser
Dim strServerName As String

Dim de As New DirectoryEntry()
Try
strServerName = "SERVER"
de.Username = "Username"
de.Password = "Password"
de.AuthenticationType = AuthenticationTypes.Secure
de.Path = "LDAP://" & strServerName & "/CN=Domain
Admins,CN=Users,DC=DomainName,DC=local"

' Invoke native method "members"
objMembers = de.Invoke("Members")
collMembers = CType(objMembers, IADsMembers)
collMembers.Filter = "user"
For Each iadsMember In collMembers
Debug.WriteLine("Name = " & iadsMember.Name)
Next

Catch ex As COMException
Debug.WriteLine("**Exception**" & vbCrLf & ex.ToString)
End Try
End Sub
--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

"BH Jodo Kast" <be**************@yahoo.com> wrote in message
news:11**********************@t39g2000cwt.googlegr oups.com...
Hi,

I found this handy script and I'm trying to convert it to VB.NET. It
pops up a list of members in the Administrators/Builtin group. Can't
seem to get DirectorySearcher or DirectoryEntry working similar to
this. Run this as a VBS script to see:

Option Explicit
Dim strUser, strMember, strDNSDomain, strContainer
Dim objGroup, objUser, objRootDSE
Dim arrMemberOf

' Bind to Active Directory'
strContainer = "cn=Administrators,cn=Builtin, "
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

' Get the Builtin Administrators group
Set objGroup = GetObject ("LDAP://"& strContainer & strDNSDomain)
objGroup.getInfo

arrMemberOf = objGroup.GetEx("member")

' Loop = For Each .... Next
WScript.Echo "Members of Group " & strContainer
For Each strMember in arrMemberOf
WScript.echo strMember
Next

Wscript.Quit

Thanks for your help!

Mar 3 '06 #2
Namespace or Type 'ActiveDs' for Imports ActiveDs cannot be found
Type IADsMembers not defined
Type IADsUsers not defined

What's ActiveDs?

Mar 3 '06 #3
Interop.ActiveDs

I added this COM reference. It's the "Active DS IIS Namespace
Provider" in Visual Studio.

Mar 3 '06 #4
I'm not sure what to specify for Server.

DC=website,DC=com?

Mar 3 '06 #5
your local domain name....

your local extension.

So if your active directory is called

TEST.DS

DC=TEST
DC=DS

--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

"BH Jodo Kast" <be**************@yahoo.com> wrote in message
news:11*********************@z34g2000cwc.googlegro ups.com...
I'm not sure what to specify for Server.

DC=website,DC=com?

Mar 3 '06 #6
Tried various server names. Not working for me.

LDAP://" & strServerName & "/CN=Domain
Admins,CN=Users,DC=DomainName,DC=local"

The script above references RootDSE. It references a domain, not a
server:
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

where I get: DC=website,DC=corp,DC=websiteusa,DC=com

Thanks for your response anyway!

Mar 3 '06 #7
DC=website,DC=corp,DC=websiteusa,DC=com

does not work. This is the domain used in the VBS script above, but
when I use it for .NET it raises an exception.

(names changed to protect the innocent :)

Mar 3 '06 #8
"LDAP://DC=website,DC=corp,DC=websiteusa,DC=com/CN=Domain
Admins,CN=Users,DC=DomainName,DC=local"

Path used. Works fine in the script... strange!

Mar 3 '06 #9
BTW This works fine:

Dim oDirent As DirectoryEntry = New
DirectoryEntry("LDAP://DC=website,DC=corp,DC=websiteusa,DC=com")
Dim oent As DirectoryEntry
For Each oent In oDirent.Children
Response.Write(oent.Name & ":" & oent.SchemaClassName &
"<BR>")
Next

The LDAP specified is a valid domain. I'm not working with servers at
this point.

This code works, but I'm looking for the members of the Administrator
group, not a list of all groups.

Thanks for your time!

Mar 3 '06 #10
"LDAP://" & strServerName & "/CN=Domain
Admins,CN=Users,DC=DomainName,DC=local"

Note the "Domain Admins" name.
--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

"BH Jodo Kast" <be**************@yahoo.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
BTW This works fine:

Dim oDirent As DirectoryEntry = New
DirectoryEntry("LDAP://DC=website,DC=corp,DC=websiteusa,DC=com")
Dim oent As DirectoryEntry
For Each oent In oDirent.Children
Response.Write(oent.Name & ":" & oent.SchemaClassName &
"<BR>")
Next

The LDAP specified is a valid domain. I'm not working with servers at
this point.

This code works, but I'm looking for the members of the Administrator
group, not a list of all groups.

Thanks for your time!

Mar 3 '06 #11
Yes it says "Domain Admins".

??

Mar 3 '06 #12
This is the line it stops at:

objMembers = de.Invoke("Members")

Error Msg:
**Exception** System.Runtime.InteropServices.COMException (0x80005000):
Unknown error (0x80005000) at
System.DirectoryServices.DirectoryEntry.Bind(Boole an throwIfFail) at
System.DirectoryServices.DirectoryEntry.Bind() at
System.DirectoryServices.DirectoryEntry.get_Native Object() at
System.DirectoryServices.DirectoryEntry.Invoke(Str ing methodName,
Object[] args) at pgSrvrBuild.LDAPtest.Page_Load(Object sender,
EventArgs e) in
\\webserver.com\wwwroot$\web\Test2\LDAPtest.aspx.v b:line 53

Mar 3 '06 #13
Try this....

Dim myOU As DirectoryEnTry = New DirectoryEnTry(LDAP://ou=Domain
Admins,dc=DOMAIN,dc=EXT)

Dim dsUsers As DirectorySearcher = New DirectorySearcher(myOU)

dsUsers.SearchScope = SearchScope.Subtree
dsUsers.Filter = "(objectCategory=Person)"

dsUsers.PropertiesToLoad.Add("displayName")
dsUsers.PropertiesToLoad.Add("givenName")
dsUsers.PropertiesToLoad.Add("sn")

Dim oSR As SearchResult
For Each oSR In dsUsers.FindAll()
Debug.Writeline(oSR.Properties("displayName"¨)(0). ToString()
Next
--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

"BH Jodo Kast" <be**************@yahoo.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
BTW This works fine:

Dim oDirent As DirectoryEntry = New
DirectoryEntry("LDAP://DC=website,DC=corp,DC=websiteusa,DC=com")
Dim oent As DirectoryEntry
For Each oent In oDirent.Children
Response.Write(oent.Name & ":" & oent.SchemaClassName &
"<BR>")
Next

The LDAP specified is a valid domain. I'm not working with servers at
this point.

This code works, but I'm looking for the members of the Administrator
group, not a list of all groups.

Thanks for your time!

Mar 3 '06 #14
Tested your new script. Added quotes around the LDAP directory name.
Results are:
"TEST**Exception** System.Runtime.InteropServices.COMException
(0x80072030): There is no such object on the server at
System.DirectoryServices.DirectoryEntry.Bind(Boole an throwIfFail) at
System.DirectoryServices.DirectoryEntry.Bind() at
System.DirectoryServices.DirectoryEntry.get_AdsObj ect() at
System.DirectoryServices.DirectorySearcher.FindAll (Boolean
findMoreThanOne) at
System.DirectoryServices.DirectorySearcher.FindAll () at "

Right now not looking for "ou=Domain Admins" I am looking for
"cn=Administrators,cn=Builtin," I changed your code to:
"LDAP://cn=Administrators,cn=Builtin,dc=DOMAIN,dc=EXT" ... no Error,
but no results.

After debugging, looks like the filter is removing all the results?
Removed the filter. Still removing results. Commented out the
"PropertiesToLoad"... no luck.

Using the VBS script above, I get 4 admins for Administrators/Builtin.

Mar 3 '06 #15
Dim myOU As DirectoryServices.DirectoryEntry = New
DirectoryServices.DirectoryEntry("LDAP://cn=Domain
Admins,cn=Users,dc=DOMAIN,dc=EXT")
Dim dsUsers As DirectoryServices.DirectorySearcher = New
DirectoryServices.DirectorySearcher(myOU)
dsUsers.SearchScope = DirectoryServices.SearchScope.Subtree
dsUsers.Filter = "(objectCategory=Person)"
dsUsers.PropertiesToLoad.Add("displayName")
dsUsers.PropertiesToLoad.Add("givenName")
dsUsers.PropertiesToLoad.Add("sn")
Dim oSR As DirectoryServices.SearchResult
For Each oSR In dsUsers.FindAll()
Debug.WriteLine(oSR.Properties("displayName")(0).T oString())
Next

--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

"BH Jodo Kast" <be**************@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
This is the line it stops at:

objMembers = de.Invoke("Members")

Error Msg:
**Exception** System.Runtime.InteropServices.COMException (0x80005000):
Unknown error (0x80005000) at
System.DirectoryServices.DirectoryEntry.Bind(Boole an throwIfFail) at
System.DirectoryServices.DirectoryEntry.Bind() at
System.DirectoryServices.DirectoryEntry.get_Native Object() at
System.DirectoryServices.DirectoryEntry.Invoke(Str ing methodName,
Object[] args) at pgSrvrBuild.LDAPtest.Page_Load(Object sender,
EventArgs e) in
\\webserver.com\wwwroot$\web\Test2\LDAPtest.aspx.v b:line 53

Mar 3 '06 #16
hang in there working on it.

--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

"BH Jodo Kast" <be**************@yahoo.com> wrote in message
news:11**********************@t39g2000cwt.googlegr oups.com...
Tested your new script. Added quotes around the LDAP directory name.
Results are:
"TEST**Exception** System.Runtime.InteropServices.COMException
(0x80072030): There is no such object on the server at
System.DirectoryServices.DirectoryEntry.Bind(Boole an throwIfFail) at
System.DirectoryServices.DirectoryEntry.Bind() at
System.DirectoryServices.DirectoryEntry.get_AdsObj ect() at
System.DirectoryServices.DirectorySearcher.FindAll (Boolean
findMoreThanOne) at
System.DirectoryServices.DirectorySearcher.FindAll () at "

Right now not looking for "ou=Domain Admins" I am looking for
"cn=Administrators,cn=Builtin," I changed your code to:
"LDAP://cn=Administrators,cn=Builtin,dc=DOMAIN,dc=EXT" ... no Error,
but no results.

After debugging, looks like the filter is removing all the results?
Removed the filter. Still removing results. Commented out the
"PropertiesToLoad"... no luck.

Using the VBS script above, I get 4 admins for Administrators/Builtin.

Mar 3 '06 #17
Same error as before. Here's what I'm trying to do:

strContainer = "cn=Administrators,cn=Builtin, "
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
'This gives me the DC=, DC=, etc.
'This is important because sometimes my domain controller for Active
Directory changes.

Set objGroup = GetObject ("LDAP://"& strContainer & strDNSDomain)
objGroup.getInfo

arrMemberOf = objGroup.GetEx("member")
'Each member is added to the array...

' Loop = For Each .... Next
WScript.Echo "Members of Group " & strContainer
For Each strMember in arrMemberOf
WScript.echo strMember
'This shows all 4 members in the array.

Re: your code, I appreciate your help. What does this refer to?
"cn=Domain Admins,cn=Users" Does not correlate with my VBS script.

Also, this:
dsUsers.PropertiesToLoad.Add("displayName")
dsUsers.PropertiesToLoad.Add("givenName")
dsUsers.PropertiesToLoad.Add("sn")
Not sure what this does.

Mar 3 '06 #18
Try This. In ou put the organizationunit you put your people in.dc is the
name of your local server domain. dc is the extension.

If you don't have any organizational units (if not we need to talk some
more) then this path changes to cn=users.

e.g. My server domain is JJP and its extension is ds thus its name is JJP.ds

domain = JJP

dc=ext

So your line roughly translated is
Dim myOU As DirectoryServices.DirectoryEntry = New
DirectoryServices.DirectoryEntry("LDAP://ou=" & organizaionalunit & ",dc=" &
domain & ",dc=" & ext)

Dim myOU As DirectoryServices.DirectoryEntry = New
DirectoryServices.DirectoryEntry("LDAP://ou=organizaionalunit,dc=domain,dc=ds")
Dim dsUsers As DirectoryServices.DirectorySearcher = New
DirectoryServices.DirectorySearcher(myOU)
dsUsers.SearchScope = DirectoryServices.SearchScope.Subtree
dsUsers.Filter = "(objectCategory=Person)"
dsUsers.PropertiesToLoad.Add("displayName")
dsUsers.PropertiesToLoad.Add("givenName")
dsUsers.PropertiesToLoad.Add("sn")
dsUsers.PropertiesToLoad.Add("memberof")

Dim oSR As DirectoryServices.SearchResult

For Each oSR In dsUsers.FindAll()

If oSR.Properties.Item("memberof").Count > -1 Then
Dim i As Integer
For i = 0 To oSR.Properties.Item("memberof").Count - 1
If
oSR.Properties.Item("memberof").Item(i).ToString.I ndexOf("Administrator")
-1 Then MsgBox(oSR.Properties.Item("displayName").Item(0))
Exit For
End If
Next
End If

Next

--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

"BH Jodo Kast" <be**************@yahoo.com> wrote in message
news:11**********************@t39g2000cwt.googlegr oups.com... Tested your new script. Added quotes around the LDAP directory name.
Results are:
"TEST**Exception** System.Runtime.InteropServices.COMException
(0x80072030): There is no such object on the server at
System.DirectoryServices.DirectoryEntry.Bind(Boole an throwIfFail) at
System.DirectoryServices.DirectoryEntry.Bind() at
System.DirectoryServices.DirectoryEntry.get_AdsObj ect() at
System.DirectoryServices.DirectorySearcher.FindAll (Boolean
findMoreThanOne) at
System.DirectoryServices.DirectorySearcher.FindAll () at "

Right now not looking for "ou=Domain Admins" I am looking for
"cn=Administrators,cn=Builtin," I changed your code to:
"LDAP://cn=Administrators,cn=Builtin,dc=DOMAIN,dc=EXT" ... no Error,
but no results.

After debugging, looks like the filter is removing all the results?
Removed the filter. Still removing results. Commented out the
"PropertiesToLoad"... no luck.

Using the VBS script above, I get 4 admins for Administrators/Builtin.

Mar 3 '06 #19
Ok, the first lines I have translated are:

Dim strDomain As String
Dim rootds As New DirectoryEntry("LDAP://rootDSE")
strDomain = rootds.Properties("DefaultNamingContext")(0) 'get the name
of the domain
Dim root As New System.DirectoryServices.DirectoryEntry("LDAP://" &
strDomain)

So far so good!

Re your code, what does this mean?
dsUsers.PropertiesToLoad.Add("displayName")
dsUsers.PropertiesToLoad.Add("givenName")
dsUsers.PropertiesToLoad.Add("sn")
dsUsers.PropertiesToLoad.Add("memberof")
This code is unwarranted.

Mar 3 '06 #20
Your kidding right? I help you and you want to quibble?

I added the other fields to show you how to pull certain fields of data.
MemberOf is critical so you find those in the Administrative groups you are
seeking. Plus you don't want to load all properties if not necessary
especially if you have hundreds of users.

However you come up with your path is up to you. I am showing you how to do
it under a normal ad situation.

--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

"BH Jodo Kast" <be**************@yahoo.com> wrote in message
news:11*********************@t39g2000cwt.googlegro ups.com...
Ok, the first lines I have translated are:

Dim strDomain As String
Dim rootds As New DirectoryEntry("LDAP://rootDSE")
strDomain = rootds.Properties("DefaultNamingContext")(0) 'get the name
of the domain
Dim root As New System.DirectoryServices.DirectoryEntry("LDAP://" &
strDomain)

So far so good!

Re your code, what does this mean?
dsUsers.PropertiesToLoad.Add("displayName")
dsUsers.PropertiesToLoad.Add("givenName")
dsUsers.PropertiesToLoad.Add("sn")
dsUsers.PropertiesToLoad.Add("memberof")
This code is unwarranted.

Mar 3 '06 #21
vbnetdev,
Just asking what you mean when you include:
dsUsers.PropertiesToLoad.Add("displayName")
No response from you, that's fine.

BTW: Check this out (works pretty good and no extra code)

Dim AD As New DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer")
Dim group As DirectoryEntry =
AD.Children.Find("administrators", "group")
Dim members As Object = group.Invoke("Members", Nothing)
Dim member As Object
For Each member In CType(members, IEnumerable)
Dim x As New DirectoryEntry(member)
Response.Write(x.Name)
Next member

Simple huh? Done and done!

Mar 3 '06 #22
Glad you got it to work.

I added the properties I did so it only loaded the properties I wanted.
Otherwise it loads all hundred some of them. Your code loads everything. If
you only have a few users however that is no big deal.
--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com

"BH Jodo Kast" <be**************@yahoo.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
vbnetdev,
Just asking what you mean when you include:
dsUsers.PropertiesToLoad.Add("displayName")
No response from you, that's fine.

BTW: Check this out (works pretty good and no extra code)

Dim AD As New DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer")
Dim group As DirectoryEntry =
AD.Children.Find("administrators", "group")
Dim members As Object = group.Invoke("Members", Nothing)
Dim member As Object
For Each member In CType(members, IEnumerable)
Dim x As New DirectoryEntry(member)
Response.Write(x.Name)
Next member

Simple huh? Done and done!

Mar 3 '06 #23
That makes sense! Thanks again for your time.

Mar 3 '06 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: lastusernameleft | last post by:
anyone know how to use a directorysearcher object to enumerate the members of the administrators group for a remote machine? ex: entry = LDAP//domain/cn=machine/cn=administrators,cn=groups" ...
0
by: Jay | last post by:
Good morning, I admit, I'm stuck any help in this would be greatly appreciated. I have a searcher.filter that is currently pulling all objects of type 'person' from my Active Directory, but I...
2
by: Steve Oswald via DotNetMonster.com | last post by:
Hello! I need to get a list of all members of a specific group (whether or not the currently logged-in user is a member of that group) in a VB.NET codebehind page. I am able to get all the...
1
by: Lady_A | last post by:
Hi, I need to figure out what is the name of the administrators users group on that machine programmatically. I.e Administratos for English, Administrateurs for French and etc. How can I do...
0
by: bcanter | last post by:
I found a file on the web that will allow you to enumerate groups but it was an .hta and the top level admins won't allow this. I need to give managers access to the groups so that when a new user is...
2
by: =?Utf-8?B?am1hZ2FyYW0=?= | last post by:
I'm trying to get a list of user accounts on the local computer - the same list you see when you are about to log in to Windows XP or Vista. This needs to work on a home computer (not connected to...
2
by: =?Utf-8?B?am9obnByZW1p?= | last post by:
Hi there, I have a web app that adds user into w2k3 server and adds it to the administrator group. The code snippet is something like this: try { DirectoryEntry AD = new...
2
by: newVBNETuser | last post by:
I want to get the follow vbScript to work in VB.NET Set objRun = CreateObject("wscript.Shell") strUserName = objRun.ExpandEnvironmentStrings("%USERNAME%") Dim strComputer, member, strUsername,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.