We help IT Professionals succeed at work.
Get Started

User Search and  Disabling Application by Active Directory

687 Views
Last Modified: 2013-11-27
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.

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

Open in new window

Untitled-picture.png
Comment
Watch Question
PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE