Wednesday, March 21, 2012

Parameter in a directorysearcher.filter?

I need to add a parameter to a directorysearcher.filter rather than using
hard-coded text. I have the following code that finds all members of an AD
group and then for each of those results tries to find that users
samAccountName.

Perhaps I'm going about this incorrectly but I know if I could pass my
searcher.filter a paramter rather than typing "cn=jason", etc this code
would work, any help would be very much appreciated!:

'Get all users in the G_SCA_Change_Control_Approvers group

Dim Approvers_entry As New
DirectoryEntry("LDAP://CN=G_SCA_Change_Control_Approvers,OU=Groups,DC=sca ,DC
=hin,DC=sk,DC=ca")

Dim Approvers_result As String

Dim entry As New DirectoryEntry("LDAP://SCA")

Dim searcher As New DirectorySearcher(entry)

Dim result As SearchResult

Dim results As SearchResultCollection

searcher.PropertiesToLoad.Add("samAccountName")

For Each Approvers_result In Approvers_entry.Properties("member")

Approvers_result = Approvers_result.ToString.Split(",")(0)

Approvers_result = Approvers_result.ToString.Split("=")(1)

txtADUserGroup.Text = txtADUserGroup.Text & Approvers_result & vbNewLine

searcher.Filter = String.Format("(&(objectClass=person)(cn=<Approvers-result
needs to go here rather than having to type the cn of the user>))")

result = searcher.FindOne

txtADUserGroup.Text = txtADUserGroup.Text &
result.Properties("samAccountName")(0).ToString() & vbNewLine

Next

Thanks in advance,

JasonIn case anyone else is having this issue, I'm able to toss a variable into
my directorysearcher.filter with the following code -- This code gathers all
members of a particular group in Active Directory, finds their
samAccountName and populates a checkboxlist with these values:
'Get all users in the G_SCA_Change_Control_Approvers group

Dim Approvers_entry As New
DirectoryEntry("LDAP://CN=G_SCA_Change_Control_Approvers,OU=Groups,DC=sca ,DC
=hin,DC=sk,DC=ca")

Dim Approvers_result As String

Dim entry As New DirectoryEntry("LDAP://SCA")

Dim searcher As New DirectorySearcher(entry)

Dim result As SearchResult

Dim results As SearchResultCollection

searcher.PropertiesToLoad.Add("samAccountName")

'Get the members of the group

For Each Approvers_result In Approvers_entry.Properties("member")

Approvers_result = Approvers_result.ToString.Split(",")(0)

Approvers_result = Approvers_result.ToString.Split("=")(1)

'Find the samAccountName of the current Approvers_result

searcher.Filter = ("(&(objectClass=person)(cn=" & Approvers_result & "))")

result = searcher.FindOne

'Fill Approvers checkbox lists with member's samAccountName property

cblApprovers.Items.Add(New
ListItem(result.Properties("samAccountName")(0).ToString))

Next

"Jay" <jgpassport@.scf.sk.ca> wrote in message
news:eBSFvDnIEHA.2244@.TK2MSFTNGP09.phx.gbl...
> I need to add a parameter to a directorysearcher.filter rather than using
> hard-coded text. I have the following code that finds all members of an
AD
> group and then for each of those results tries to find that users
> samAccountName.
> Perhaps I'm going about this incorrectly but I know if I could pass my
> searcher.filter a paramter rather than typing "cn=jason", etc this code
> would work, any help would be very much appreciated!:
> 'Get all users in the G_SCA_Change_Control_Approvers group
> Dim Approvers_entry As New
DirectoryEntry("LDAP://CN=G_SCA_Change_Control_Approvers,OU=Groups,DC=sca ,DC
> =hin,DC=sk,DC=ca")
> Dim Approvers_result As String
> Dim entry As New DirectoryEntry("LDAP://SCA")
> Dim searcher As New DirectorySearcher(entry)
> Dim result As SearchResult
> Dim results As SearchResultCollection
> searcher.PropertiesToLoad.Add("samAccountName")
> For Each Approvers_result In Approvers_entry.Properties("member")
> Approvers_result = Approvers_result.ToString.Split(",")(0)
> Approvers_result = Approvers_result.ToString.Split("=")(1)
> txtADUserGroup.Text = txtADUserGroup.Text & Approvers_result & vbNewLine
> searcher.Filter =
String.Format("(&(objectClass=person)(cn=<Approvers-result
> needs to go here rather than having to type the cn of the user>))")
> result = searcher.FindOne
> txtADUserGroup.Text = txtADUserGroup.Text &
> result.Properties("samAccountName")(0).ToString() & vbNewLine
> Next
>
> Thanks in advance,
> Jason

0 comments:

Post a Comment