Display operatingSystem attribute in a message box RRS feed

  • Question

  • I am having trouble getting the operatingSystem property for computers in Active Directory(AD).  Here is the code I am having trouble with and the function that gets the property from AD.  The hostname works perfectly but the OS_Name displays a blank message box.

    Alternatively, I would also like help formatting the filter to make things easier down the road.  My goal is to check the operating system and only process the Windows 10 Enterprise and Windows 7 Enterprise operating systems but I can't even display the operating system in a message box.  Please help.

    Dim enTry As DirectoryEntry = New DirectoryEntry("GC://,DC=my,DC=work,DC=com")
                Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
                mySearcher.Filter = ("(objectClass=computer)")
                Dim ds As SearchResult
                For Each ds In mySearcher.FindAll()
                    Dim OS_Name As String = GetProperty(ds, "operatingSystem")
                    Dim hostName As String = GetProperty(ds, "dNSHostName")
     Function GetProperty(ByVal searchResult As SearchResult, ByVal PropertyName As String)
            If searchResult.Properties.Contains(PropertyName) Then
                Return searchResult.Properties(PropertyName)(0).ToString()
                Return String.Empty
            End If
        End Function


    Tuesday, September 10, 2019 6:42 PM

All replies

  • Hi,

    "operatingsystem" instead of "operatingSystem" or try the following code:

      Dim OperatingSystem As String
            Dim ser As DirectorySearcher = New DirectorySearcher(enTry)
                ser.Filter = "(&ObjectCategory=computer)"
                Dim results As SearchResultCollection = ser.FindAll()
                For Each res As SearchResult In results
                    For Each key As String In res.Properties.PropertyNames
                        For Each myCollection As Object In res.Properties(key)
                        If key.ToString() = "operatingsystem" Then
                            End If

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, September 11, 2019 3:20 AM
  • Thanks for the response. I tried your code example and it didn't work. Nothing happened... no message and no errors.  I tried making this change and it looks it is doing what is intended but gets an error.

    Exception thrown: 'System.InvalidCastException' in Microsoft.VisualBasic.dll

    Conversion from string "samaccountname" to type 'Integer' is not valid.

    For Each myCollection As Object In res.Properties.PropertyNames(key)
                                If key.ToString() = "operatingsystem" Then
                                End If


    Wednesday, September 11, 2019 2:56 PM