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
|