Solved

Use  .PropertiesToLoad.Add("nvdPolicy")

Posted on 2010-08-12
7
508 Views
Last Modified: 2013-11-07
I have a path in active directory and need to get some values for that path with .net 2.0.  I can see these values with adsi edit, but would like to populate a combobox with them.

Here is what I have, but it's not working.  What am I missing?
  Dim strSoftType As String = ""
        If RadioButton2.Checked = True Then
            strSoftType = "OU=Software,"
        ElseIf RadioButton1.Checked = True Then
            strSoftType = "OU=Softserv,"
        End If
        Dim SoftName = "OU=" & ComboBox1.Text.Trim & ","
        Dim strTypeInstall As String = "CN=" & ComboBox2.Text.Trim & ","
        ComboBox3.Items.Clear()

        Dim strSoftwareLoc, strSoftwareName As DirectoryEntry
       


        Dim strResult As SearchResult
        Dim strSoftwareList As String = ""
        strSoftwareLoc = New DirectoryEntry("LDAP://" & strTypeInstall & SoftName & strSoftType & "OU=Radia,OU=Applications,DC=mydomain,DC=com")
        Dim strSearch As New DirectorySearcher(strSoftwareLoc)

        With strSearch
            .SearchScope = SearchScope.Subtree
            .PropertiesToLoad.Add("nvdPolicy")
            .PropertiesToLoad.Add("nvdPolicyOverride")
        End With

        strSearch.SearchRoot = strSoftwareLoc

        For Each strResult In strSearch.

            Dim title As DirectoryEntry
            title = strResult.GetDirectoryEntry.Properties("nvdPolicy").Value '***************** I FAIL HERE




            ComboBox3.Items.Add(title)    'add to combobox and remove OU=

        Next
0
Comment
Question by:chadmanvb
  • 4
  • 3
7 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33429361
for better practice u first want to check if property is exists, for instance:

    Dim title As DirectoryEntry
dim objVal  as object
objVal = strResult.GetDirectoryEntry.Properties("nvdPolicy")
if not objVal  = nothing then
            title = objVal.Value
else
'property nvdPolicy doesn't exists
end if
0
 

Author Comment

by:chadmanvb
ID: 33429809
I get this error:
Operator '=' is not defined for type 'PropertyValueCollection' and 'Nothing'.

on line: if not objVal  = nothing then

any ideas?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33429898
try debug your code to till this line:
objVal = strResult.GetDirectoryEntry.Properties("nvdPolicy")

what is the value of objVal?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33430026
try the following:

With strSearch
            .SearchScope = SearchScope.Subtree
            .PropertiesToLoad.Add("nvdPolicy")
            .PropertiesToLoad.Add("nvdPolicyOverride")
        End With

        strSearch.SearchRoot = strSoftwareLoc

        For Each strResult In strSearch.FindAll()
            For Each item In strResult.GetDirectoryEntry().Properties("nvdPolicy")

            Next
 For Each item In strResult.GetDirectoryEntry().Properties("nvdPolicyOverride")

            Next
        Next
0
 

Author Comment

by:chadmanvb
ID: 33430550
Looks like I am still getting the same error I originally had:

Unable to cast object of type 'System.String' to type 'System.DirectoryServices.DirectoryEntry'.

 With strSearch
            .SearchScope = SearchScope.Subtree
            .PropertiesToLoad.Add("nvdPolicy")
            .PropertiesToLoad.Add("nvdPolicyOverride")
        End With

        strSearch.SearchRoot = strSoftwareLoc

        For Each strResult In strSearch.FindAll()
            Dim title As DirectoryEntry
            For Each item In strResult.GetDirectoryEntry().Properties("nvdPolicy")


                Dim objVal As Object
                objVal = strResult.GetDirectoryEntry.Properties("nvdPolicy")

                title = objVal.Value  '*******************fails here


            Next

            For Each item In strResult.GetDirectoryEntry().Properties("nvdPolicyOverride")


            Next
            ComboBox3.Items.Add(title)    'add to combobox and remove OU=
        Next
0
 

Author Comment

by:chadmanvb
ID: 33431171
When I change the variable to Dim strAttributeValue As Object

It works fine.  Can a directory value not be converted to a string?
0
 

Author Closing Comment

by:chadmanvb
ID: 33435944
Thanks, that's working.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now