|
-
Jul 16th, 2004, 01:27 AM
#1
Thread Starter
Hyperactive Member
Searching AD with VB .NET
Does anybody know of a way to search Active Directory for a given string (say a users surname or part of it)?
I have this code from Microsoft's Scripting Center:-
Code:
Dim objConnection = CreateObject("ADODB.Connection")
objConnection.Open("Provider=ADsDSOObject;")
Dim objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"<LDAP://dc=npf,dc=internal>;(&(objectCategory=User)" & _
"(displayName=" & SearchParameter & "));displayName;subtree"
Dim objRecordSet = objCommand.Execute
If objRecordSet.RecordCount = 0 Then
MessageBox.Show("sAMAccountName: " & SearchParameter & " does not exist.")
Else
MessageBox.Show(SearchParameter & " exists " & objRecordSet.RecordCount & " time(s).")
End If
objConnection.Close()
(the SearchParameter is passed to the function and does work if you enter wildcards characters).
However, there doesn't seem to be a way to interrogate the objRecordSet object more fully, say for the users actual name, user id etc etc.
I can only return a recordcount, not the values contained within it.
"I'm Brian and so is my Wife"
-
Oct 5th, 2004, 11:27 PM
#2
Addicted Member
Here is some code I use to logon to a progam we wrote...
Also, at the bottom, are some additional AD fields you can display/search...
Code:
Dim strDomain As String
Dim rootds As New DirectoryEntry("LDAP://rootDSE")
strDomain = rootds.Properties("DefaultNamingContext")(0) 'get the name of the domain
Dim root As New System.DirectoryServices.DirectoryEntry("LDAP://" & strDomain)
Dim searcher As New System.DirectoryServices.DirectorySearcher(root)
searcher.Filter = "(&(objectCategory=user)(anr=" & txtusername.Text & "))"
searcher.PropertiesToLoad.Add("cn")
Dim results As SearchResultCollection
results = searcher.FindAll()
'Loop though the matches.
Dim result As SearchResult
For Each result In results
Dim User As New System.DirectoryServices.DirectoryEntry(result.Path)
Label4.Text = "Welcome " & (User.Properties("cn").Item(0)) ' display a message box for each match
User = Nothing
Next result
'Dim strDomain As String
'Dim rootds As New DirectoryEntry("LDAP://rootDSE")
strDomain = rootds.Properties("DefaultNamingContext")(0) 'get the name of the domain
'Dim root As New System.DirectoryServices.DirectoryEntry("LDAP://" & strDomain)
'Dim searcher As New System.DirectoryServices.DirectorySearcher(root)
'searcher.Filter = "(&(objectCategory=user)(anr=" & "*" & "))"
searcher.Filter = "(&(objectCategory=user))"
searcher.PropertiesToLoad.Add("cn")
searcher.PropertiesToLoad.Add("sAMAccountName")
'Dim results As SearchResultCollection
results = searcher.FindAll()
'Loop though the matches.
'Dim result As SearchResult
For Each result In results
Dim User As New System.DirectoryServices.DirectoryEntry(result.Path)
'MsgBox(User.Properties("password").Item(0)) ' display a message box for each match
'On Error GoTo noname
If User.Properties("sAMAccountName").Value = "" Then
GoTo noname
End If
If InStr(User.Properties("sAMAccountName").Value, ".", CompareMethod.Text) > 0 And (User.Properties("useraccountcontrol").Value = "66048" Or User.Properties("useraccountcontrol").Value = "512") Then
usersbox.Items.Add(User.Properties("sAMAccountName").Value)
End If
'usernamead.Items.Add(User.Properties("sn").Item(0))
noname:
User = Nothing
Next result
Field="title">{Job Title}
Field="company">{Company}
Field="physicalDeliveryOfficeName">{Office}
Field="givenName">{First Name}
Field="sn">{Last Name}
Field="distinguishedName">{Distinguished Name}
Field="homePhone">{Home Phone}
Field="facsimileTelephoneNumber">{Fax}
Field="streetAddress">{Street Address}
Field="st">{State/County}
Field="personalTitle">{Personal Title}
Field="homePostalAddress">{Home Postal Address}
Field="department">{Department}
Field="description">{Description}
Field="displayName">{Display Name}
Field="initials">{Initials}
Field="mail">{Email Address}
Field="telephoneNumber">{Telephone}
Field="mobile">{Mobile Number}
Field="wWWHomePage">{Home Page}
Field="l">{City}
Field="postalCode">{ZIP/Postcode}
Field="postalAddress">{Postal Address}
Field="info">{Notes}
Field='sAMAccountName'>{AD:sAMAccountName}
Field='accountExpires'>{AD:accountExpires}
Field='adminDescription'>{ADminDescription}
Field='countryCode'>{AD:countryCode}
Field='assistant'>{AD:assistant}
Field='division'>{AD:division}
Field='employeeID'>{AD:employeeID}
Field='employeeType'>{AD:employeeType}
Field='facsimileTelephoneNumber'>{AD:facsimileTelephoneNumber}
Field='homeDirectory'>{AD:homeDirectory}
Field='homePhone'>{AD:homePhone}
Field='ipPhone'>{AD:ipPhone}
Field='logonHours'>{AD:logonHours}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|