Last post Dec 17, 2010 01:05 PM by AkAlan
Dec 15, 2010 11:46 AM|AkAlan|LINK
I am trying to find all users from a specific Group and keep getting an error saying "There is no such object on the server". If I run my code without specifying the group I get all objects returned from AD so I think this is just a syntax error on my part.
I tried following examples from other posts that seem like they should work but I am lost at this point. Here is the code that generates the error:
Dim objSearch As New DirectorySearcher()
objSearch.SearchRoot = New DirectoryEntry("LDAP://CN=!Operations,OU=GROUPS,dc=ars,dc=local")
objSearch.Filter = "(&(objectclass=user)(objectcategory=person))"
objSearch.SearchScope = SearchScope.Subtree
Dim colQueryResults As SearchResultCollection
colQueryResults = objSearch.FindAll()
If I remove the CN= !Operations and OU = Groups, I get a result set with all accounts. I do have a group called !Operations. Could it be that the code doesn't like the exclamation mark as part of the group name? Any help would be appreciated.
Dec 16, 2010 11:10 AM|smirnov|LINK
Try to add a backslash (\) character in front of !. According to
this article, ! is not in the list of special characters, so maybe there is another problem. Read more
Also try to set "OU=Groups" instead of "OU=GROUPS".
Dec 17, 2010 01:05 PM|AkAlan|LINK
OK, I solved my issue by using a different method. I am not finished cleaning the code up but the heart of my problem is solved so I am posting what I got to work. I had to Import the following namespaces Imports System.DirectoryServices
I'm sure there are more namespaces imported than I need for this to work but I included them all here for the sake of finishing this post. The idea here is I want to be able to loop through all my users and find out the last time their password was set and
insert the info to a sql table.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lastPasswordSet As Date
Dim sqlCmd As New SqlCommand()
Dim sqlConString As String = "Data Source=My-Sql-Server;Initial Catalog=MyDatabase;Integrated Security=True"
Dim sqlConn As New SqlConnection(sqlConString) sqlConn.Open()
.Connection = sqlConn
.CommandType = CommandType.StoredProcedure
.CommandText = ("spInsertALL_LastPasswordChanged")
With Dim ctx As PrincipalContext = New PrincipalContext(ContextType.Domain, "MyDomain")
Dim group As GroupPrincipal = GroupPrincipal.FindByIdentity(ctx, "MySecurityGroup")
For Each p As Principal In group.Members
Dim up As UserPrincipal = p
Dim userName As String = p.Name
If IsDate(up.LastPasswordSet) Then
lastPasswordSet = up.LastPasswordSet
' I will be here if user must change password is set.