U
Umphorton
Guest
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://my.work.com/DC=AB27,DC=my,DC=work,DC=com")
Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
mySearcher.Filter = ("(objectClass=computer)")
mySearcher.PropertiesToLoad.Add("dNSHostName")
mySearcher.PropertiesToLoad.Add("operatingSystem")
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")
MessageBox.Show(hostName)
MessageBox.Show(OS_Name)
Next
Function GetProperty(ByVal searchResult As SearchResult, ByVal PropertyName As String)
If searchResult.Properties.Contains(PropertyName) Then
Return searchResult.Properties(PropertyName)(0).ToString()
Else
Return String.Empty
End If
End Function
Umphorton
Continue reading...
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://my.work.com/DC=AB27,DC=my,DC=work,DC=com")
Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
mySearcher.Filter = ("(objectClass=computer)")
mySearcher.PropertiesToLoad.Add("dNSHostName")
mySearcher.PropertiesToLoad.Add("operatingSystem")
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")
MessageBox.Show(hostName)
MessageBox.Show(OS_Name)
Next
Function GetProperty(ByVal searchResult As SearchResult, ByVal PropertyName As String)
If searchResult.Properties.Contains(PropertyName) Then
Return searchResult.Properties(PropertyName)(0).ToString()
Else
Return String.Empty
End If
End Function
Umphorton
Continue reading...