Question : User Search and  Disabling Application by Active Directory

I am working on creating a application to search for users with "display name" and loginid
Review the same below and disable the user,move the user to a different ou and modify the description as well.
Achieved:
  Able to search user with login name

*Troubles i am having are:
     ** Unable to make the code so that i can search with Display name
     ** Unable to get the Managers email id of the user
     **Unable to create a plan in which the disabling,moving the user to a different ou can be done.

can someone assist on the same.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
Option Explicit On
Imports System.DirectoryServices
Imports System.IO
Imports Microsoft.Win32
Imports System.Drawing
Imports Microsoft.VisualBasic
 
Public Class UTT
 
 
   Private Sub searchbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchbtn.Click
        Dim pass1 As String = ""
        Dim pass2 As String = ""
        If samidtxt.Text <> "" Or displaynametxt.Text <> "" Then
            Dim search1 = Len(samidtxt.Text)
            Dim search2 = Len(displaynametxt.Text)
            If search1 <> "0" And search2 = "0" Then pass1 = samidtxt.Text Else 
            If search2 <> "0" And search1 = "0" Then pass1 = displaynametxt.Text
            If pass1 = samidtxt.Text Then pass2 = "1"
            If pass1 = displaynametxt.Text Then pass2 = "2"
 
 
            result_displaynametext.Text = GetFields(pass1, "givenname", pass2)
            result_samidtext.Text = GetFields(pass1, "sAMAccountName", pass2)
            result_desctext.Text = GetFields(pass1, "description", pass2)
            result_emailidtext.Text = GetFields(pass1, "manager", pass2)
            result_isdisabled.Text = GetFields(pass1, "userAccountControl", pass2)
            If result_isdisabled.Text = "514" Then result_isdisabled.Text = "Yes" Else result_isdisabled.Text = "NO"
            If result_isdisabled.Text = "" Then result_isdisabled.Text = ""
            'If result_isdisabled.Text = "NO" Then result_isdisabled.BackColor = Color.Tomato Else 
            If result_isdisabled.Text = "Yes" Then tick.Visible = True Else tick.Visible = False
 
 
 
 
 
 
        End If
    End Sub
 
 
    Public Shared Function GetFields(ByVal UserName As String, ByVal neededvalue As String, ByVal pass2 As Integer) As String
 
        'Const DomainName As String = " schusterna.com"
        Dim DomainName As String = Environment.UserDomainName
        Dim oDirectory As New DirectoryEntry("LDAP://" & DomainName)
        Dim mySearcher As New DirectorySearcher(oDirectory)
        Dim oResult As SearchResult
        Dim sResult As String = ""
        mySearcher.SearchScope = SearchScope.Subtree
        mySearcher.ReferralChasing = ReferralChasingOption.All
        If pass2 = "1" Then
 
            mySearcher.Filter = "(&(objectClass=user)(sAMAccountName=" & UserName & "))"
        Else
            If pass2 = "2" Then
                mySearcher.Filter = "(&(objectClass=user)(name=" & UserName & "))"
            End If
        End If
        Try
            oResult = mySearcher.FindOne
            If Not oResult Is Nothing Then
                sResult = oResult.GetDirectoryEntry.Properties(neededvalue).Value.ToString()
                'Dim desc As String=(mySearcher., neededvalue:="Desc")
 
                
 
            End If
        Catch ex As Exception
            Throw ex
        End Try
 
        oResult = Nothing
        mySearcher.Dispose()
        oDirectory.Dispose()
        Return sResult
    End Function
 
 
 
 
 
 
 
 
End Class

Answer : User Search and  Disabling Application by Active Directory


Just a quick note first... any code examples below are from memory, nothing to test it on at the moment. I hope I didn't make any glaring errors :)

>      ** Unable to make the code so that i can search with Display name

The filter you want for searching by display name is like this

(&(objectClass=user)(objectCategory=person)(displayName= ... ))

Where you would have the code insert the value you want to search in place of "...".

>      ** Unable to get the Managers email id of the user

To get that you must either construct a search that returns the Manager, or connect to the Managers account as a DirectoryEntry.

For example, if you've take the "manager" field from a user you could run:

Dim adManager As New DirectoryEntry("LDAP://" & strManager)
Dim managerEmail As String = adManager.Properties("mail").Value.ToString()

>      **Unable to create a plan in which the disabling,moving the user to a different ou
> can be done.

Disabling can be done in one of two ways. Either modify the userAccountControl value, a bitwise comparison and addition of the decimal value 2 will disable the account.

Alternatively set the AccountDisabled Property. I believe setting the AccountDisabled Property is a bit less obvious, you may have to access it like this:

adUser.NativeObject.AccountDisabled = True

Afraid I can't test it at the moment, but I can in the morning if you're still having trouble with it.

Finally, to move an account you need to invoke the MoveHere method on the container you want to move it to. For example:

Dim adTargetOU As New DirectoryEntry("LDAP://OU=somewhereNew,DC=yourdomain,DC=com")
adTarget.Invoke("MoveHere", adUser.ADSPath)

Probably needs a bit of prodding to get it working. Need more detail? :)

Chris
Random Solutions  
 
programming4us programming4us