Imports Org.BouncyCastle.Asn1 Imports VERAG_PROG_ALLGEMEIN Public Class frmADsearch Dim dt As New DataTable() Public activeDirectoryObj = New ADObject() Public Sub New() ' Dieser Aufruf ist für den Designer erforderlich. InitializeComponent() End Sub Public Sub New(changelabel As String) ' Dieser Aufruf ist für den Designer erforderlich. InitializeComponent() lblUser.Text = changelabel End Sub Sub initDataTable() dt.Columns.Add("sAMAccountName", GetType(String)) dt.Columns.Add("givenName", GetType(String)) dt.Columns.Add("sn", GetType(String)) dt.Columns.Add("DisplayName", GetType(String)) dt.Columns.Add("distinguishedName", GetType(String)) dt.Columns.Add("department", GetType(String)) dt.Columns.Add("company", GetType(String)) dt.Columns.Add("mail", GetType(String)) dt.Columns.Add("cn", GetType(String)) dt.Columns.Add("telephoneNumber", GetType(String)) dt.Columns.Add("extensionAttribute7", GetType(String)) dt.Columns.Add("adEntry", GetType(System.DirectoryServices.DirectoryEntry)) End Sub Sub initDGV(dt As DataTable) dgvAD.DataSource = dt If dgvAD.Columns.Count > 0 Then With dgvAD .RowHeadersWidth = 10 .Columns("GivenName").HeaderText = "Firstname" .Columns("GivenName").Width = 75 .Columns("sn").Width = 100 .Columns("sn").HeaderText = "Lastname" .Columns("DisplayName").Visible = False .Columns("sAMAccountName").Width = 75 .Columns("sAMAccountName").HeaderText = "Account" .Columns("department").Width = 75 .Columns("distinguishedName").HeaderText = "DN-Name" .Columns("distinguishedName").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill .Columns("department").HeaderText = "Department" .Columns("company").Width = 75 .Columns("company").HeaderText = "Company" .Columns("telephoneNumber").Visible = False .Columns("extensionAttribute7").Visible = False .Columns("cn").Visible = False End With End If End Sub Private Sub GetActiveDirUserDetails(ByVal username As String, Optional LDAPString As String = "LDAP://DC=VERAG,DC=OST,DC=DMN") dt.Clear() Dim dirEntry As System.DirectoryServices.DirectoryEntry Dim dirSearcher As System.DirectoryServices.DirectorySearcher Try dirEntry = New System.DirectoryServices.DirectoryEntry(LDAPString) dirSearcher = New System.DirectoryServices.DirectorySearcher(dirEntry) dirSearcher.Filter = "(samAccountName=" & username & ")" dirSearcher.PropertiesToLoad.Add("GivenName") dirSearcher.PropertiesToLoad.Add("sn") Dim sr As DirectoryServices.SearchResultCollection = dirSearcher.FindAll() If sr Is Nothing Then lblHint.Text = "No Entries found" Exit Sub ElseIf sr.Count = 0 Then lblHint.Text = sr.Count & " Entries found" End If For Each srEntry As DirectoryServices.SearchResult In sr Dim de As System.DirectoryServices.DirectoryEntry de = srEntry.GetDirectoryEntry() 'Dim ObjFirstName As String = "" 'Dim ObjLastName As String = String.Empty Dim R As DataRow = dt.NewRow R("givenName") = de.Properties("givenName").Value.ToString() R("sAMAccountName") = de.Properties("sAMAccountName").Value.ToString() R("department") = de.Properties("department").Value.ToString() R("company") = de.Properties("company").Value.ToString() R("sn") = de.Properties("sn").Value.ToString() R("cn") = de.Properties("cn").Value.ToString() R("telephoneNumber") = de.Properties("telephoneNumber").Value.ToString() R("distinguishedName") = de.Properties("distinguishedName").Value.ToString() R("extensionAttribute7") = de.Properties("extensionAttribute7").Value.ToString() R("adEntry") = de dt.Rows.Add(R) Next Catch ex As Exception lblHint.Text = ex.Message End Try End Sub Public Shared Function GetUsersInGroup(ByVal groupname As String, Optional ByVal LDAP As String = "LDAP://DC=VERAG,DC=OST,DC=DMN") As List(Of String) Dim Userlist As New List(Of String) Dim locDirectoryEntry As New DirectoryServices.DirectoryEntry(LDAP) Dim Searcher As New DirectoryServices.DirectorySearcher(locDirectoryEntry, "sAMAccountName=" & groupname) Dim Result As DirectoryServices.SearchResult = Searcher.FindOne If Result IsNot Nothing Then For Each User In Result.Properties("Member") Userlist.Add(User) Next End If Return Userlist End Function Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click lblHint.Text = "" If cboDomain._value = "" Then lblHint.Text = "Domain auswählen" Exit Sub End If If txtUser.Text = "" Then lblHint.Text = "User eingeben!" Exit Sub End If GetActiveDirUserDetails(txtUser.Text) End Sub Private Sub frmADsearch_Load(sender As Object, e As EventArgs) Handles MyBase.Load cboDomain.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAGOST", "LDAP://DC=VERAG,DC=OST,DC=DMN")) cboDomain.changeItem("verag.ost.dmn") initDataTable() initDGV(dt) End Sub Private Sub btnUebernehmen_Click(sender As Object, e As EventArgs) Handles btnUebernehmen.Click For Each row As DataGridViewRow In dgvAD.SelectedRows activeDirectoryObj = New ADObject With { .sn = IIf(IsDBNull(row.Cells("sn").Value), "", row.Cells("sn").Value), .sAMAccountName = IIf(IsDBNull(row.Cells("sAMAccountName").Value), "", row.Cells("sAMAccountName").Value), .givenName = IIf(IsDBNull(row.Cells("givenName").Value), "", row.Cells("givenName").Value), .cn = IIf(IsDBNull(row.Cells("cn").Value), "", row.Cells("cn").Value), .department = IIf(IsDBNull(row.Cells("department").Value), "", row.Cells("department").Value), .company = IIf(IsDBNull(row.Cells("company").Value), "", row.Cells("company").Value), .telephoneNumber = IIf(IsDBNull(row.Cells("telephoneNumber").Value), "", row.Cells("telephoneNumber").Value), .distinguishedName = IIf(IsDBNull(row.Cells("distinguishedName").Value), "", row.Cells("distinguishedName").Value), .mail = IIf(IsDBNull(row.Cells("mail").Value), "", row.Cells("mail").Value), .domain = cboDomain.Text, .emailExtension = IIf(IsDBNull(row.Cells("extensionAttribute7").Value), "", row.Cells("extensionAttribute7").Value), .ADEntry = IIf(IsDBNull(row.Cells("adEntry").Value), "", row.Cells("adEntry").Value) } Next End Sub End Class Public Class ADObject Public Sub New() End Sub Public Property sn As String Public Property givenName As String Public Property cn As String Public Property department As String Public Property company As String Public Property telephoneNumber As String Public Property distinguishedName As String Public Property mail As String Public Property sAMAccountName As String Public Property domain As String Public Property emailExtension As String Public Property ADEntry As System.DirectoryServices.DirectoryEntry End Class